我在将ArrayList
自定义类写入文件时遇到问题。我正在使用
public static void SaveEmployees() throws FileNotFoundException, IOException{
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("magicscheduleEmp.sav");
save.writeObject(employeeList);
save.close();
}
在main方法中声明employeeList
并将其初始化为ArrayList
{我的类}的Employees
。我确实已将对象添加到employeeList
,但调用SaveEmployees
会将空ArrayList
写入文件。 Employee
类是可序列化的。
我做错了什么?
编辑:在此处初始化employeeList(在主方法中)
public static ArrayList<Employee> employeeList = new ArrayList<>();
并通过用户输入(在另一个jFrame类中)填充
if(MainUI.addempcheck == true){
Employee newEmployee = new Employee(LastName.getText(), FirstName.getText());
newEmployee.position = EmpType.getSelectedItem().toString();
MainUI.employeeList.add(newEmployee);
MainUI.listModel.addElement(newEmployee.writeName().toString());
}
try {
MainUI.SaveEmployees();
} catch (FileNotFoundException ex) {
Logger.getLogger(NewEmployeeUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewEmployeeUI.class.getName()).log(Level.SEVERE, null, ex);
}
this.dispose();
我有一个打印employeeList内容的按钮,并显示在保存之前填充列表。也许这是调用SaveEmployees的问题?