ObjectOutputStream ObjectInputStream 对象序列化

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        writerObj();
        readerObj();
    }

    private static void readerObj() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("obj.object"));
        person p=(person) ois.readObject();
        System.out.println(p.getName()+":"+p.getAge());
    }

    public static void writerObj() throws IOException, FileNotFoundException {
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("obj.object"));
        oos.writeObject(new person("wangwu",22));
        oos.close();
    }
public class person implements Serializable/*标记接口*/{

    /**
     * 
     */
    private static final long serialVersionUID = 9527;
    public person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }

}

posted on 2012-10-22 20:57  蝌蚪的精神  阅读(111)  评论(0编辑  收藏  举报

导航