如果我再次写入一个student实例呢?
好像原对象被覆盖了?
好像原对象被覆盖了?
2016-02-17
public class ObjectToByteDemo { public static void main(String[] args){ try { ObjectOutputStream objectWriter =new ObjectOutputStream( new FileOutputStream("testFolder/objectTestDemo.txt")); StudentDemo st =new StudentDemo("XiaoMing","45398",19); StudentDemo st01 =new StudentDemo("Chen","87659",22); objectWriter.writeObject(st); objectWriter.writeObject(st01); objectWriter.flush(); objectWriter.close(); ObjectInputStream objectReader = new ObjectInputStream( new FileInputStream("testFolder/objectTestDemo.txt")); StudentDemo tmpStu =(StudentDemo)objectReader.readObject(); System.out.println(tmpStu); //Return the object read from the stream StudentDemo tmpStu01 =(StudentDemo)objectReader.readObject(); System.out.println(tmpStu01); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
结果:
StudentDemo [stuName=XiaoMing, stuID=45398, age=19] StudentDemo [stuName=Chen, stuID=87659, age=22]
不会被覆盖,可以理解为像read()方法一样,指针从0开始,偏移一个读一个对象出来。
不能指定反序列哪一个对象。
举报