bryan4it
2016-02-17 11:41
好像原对象被覆盖了?
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开始,偏移一个读一个对象出来。
不能指定反序列哪一个对象。
如果想一次都出来完,就用一个集合,对其收集;然后在序列化
没有被覆盖。应该是一次只能传输一个序列化对象。

文件传输基础——Java IO流
133837 学习 · 1060 问题
相似问题