我有一个 Employee 对象的 ArrayList,其中 Employee 类实现了 Serializable。我正在使用此代码将列表写入文件:
ArrayList<Employee> empList = new ArrayList<>();
FileOutputStream fos = new FileOutputStream("EmpObject.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// write object to file
empList .add(emp1);
empList .add(emp2);
oos.writeObject(empList);
empList .add(emp3);
oos.writeObject(empList);
}
如果我尝试反序列化它,我只会得到前两个对象而不是第三个对象。任何人都可以试试这是为什么?
编辑1:如果我一次添加所有元素,一切都很好,但不是我第一次做的方式。有什么区别?
ArrayList<Employee> empList = new ArrayList<>();
FileOutputStream fos = new FileOutputStream("EmpObject.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// write object to file
empList .add(emp1);
empList .add(emp2);
empList .add(emp3);
oos.writeObject(empList);
}
在此之后我有 3 个元素
炎炎设计
Helenr
慕哥9229398
相关分类