0

我无法将ArrayList自定义类写入文件。我在用着

public static void SaveEmployees() throws FileNotFoundException, IOException{
    ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("magicscheduleEmp.sav");
    save.writeObject(employeeList);
    save.close();
}

where在 main 方法中employeeList被声明并初始化为ArrayList(我的类)的一个。Employees我已经明确添加了对象employeeList,但是调用会向文件中SaveEmployees写入一个空ArrayList的内容。该类Employee是可序列化的。

我究竟做错了什么?

编辑:employeeList 在此处初始化(在 main 方法中)

    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 有问题?

4

0 回答 0