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; }}