日期:2014-05-20 浏览次数:20817 次
public class 对象流 { public static void main(String[] args) { try { /*ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("c:/a",true)); User u1 = new User("zs"); User u2 = new User("ls"); os.writeObject(u1); os.writeObject(u2); os.flush(); os.close();*/ ObjectInputStream is = new ObjectInputStream(new FileInputStream("c:/a")); Object obj1 = is.readObject(); Object obj2 = is.readObject(); Object obj3 = is.readObject(); System.out.println(obj1); System.out.println(obj2); System.out.println(obj3); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } class User implements Serializable{ String name; User(String name){ this.name = name; } @Override public String toString() { return "User[name = "+this.name; } }