如何保存和加载对象的数组列表?

时间:2019-01-19 11:05:53

标签: java arraylist save load

我有一个类Employees,它具有在数组列表中添加,删除,显示和搜索雇员的方法。现在,我正在尝试创建一个文件处理程序来保存和读取此数据,但是由于某种原因,它无法正常工作,也无法创建或读取文件。谁能找到我的代码有问题吗?

这是雇员类

//This creates an array of employees
static ArrayList<Employees> employee = new ArrayList<Employees>();

//This method returns a employee's name
public String getName(){
    return Name;
}

//This method returns a employee's surname
public String getSurname(){
    return Surname;
}

//This method returns a employee's ID number
public String getID(){
    return ID;
}

//This method returns a employee's mobile number
public int getMobNo(){
    return MobNo;
}

这是Employee File Handler,保存和加载方法为:

import java.util.*;
import java.io.*;

public class EmployeeFileHandler implements Serializable{

 public static void readData(ArrayList <Employees> listIn){

    boolean endOfFile = false;
    Employees temp;

    try{

        FileInputStream employeesFile = new FileInputStream 
        ("employee.obf");
        ObjectInputStream employeesStream = new ObjectInputStream 
        (employeesFile);
        temp = (Employees) employeesStream.readObject();        

     while(endOfFile != true){
      try{
         listIn.add(temp);
         temp = (Employees) employeesStream.readObject();        
      }
      catch(EOFException e){
         endOfFile = true;
      }            
     }
     employeesStream.close();
    }

    catch(FileNotFoundException e){
        System.out.println("Employee file was not found");
    }
    catch (IOException e){             
         System.out.println("Employee file could not be read");
    }
    catch (ClassNotFoundException e){             
         System.out.println("Employee class was not found");             

2 个答案:

答案 0 :(得分:0)


我测试了您的代码,但未发现任何问题。
为什么不尝试捕获Throwable并通过调试对其进行检查?

try { /* code */ }
catch (Throwable t) { t.printStackTrace(); }

答案 1 :(得分:0)

此解决方案测试EmployeeFileHandler并在例如目录不可写的情况下显示错误。如果断言失败,请与javac EmployeeFileHandler.java; java -ea EmployeeFileHandler一起运行以使其崩溃。确保Person类也可序列化。

import java.util.*;
import java.io.*;

public class EmployeeFileHandler implements Serializable {
    public static void main(String[] args) {
        ArrayList<Employees> employees = new ArrayList<Employees>();
        for (int i = 0; i < 1000; i++) {
            employees.add(new Employees("Name" + i, "Surname" + i, "" + i, 12345678));
        }        
        writeDate(employees);

        ArrayList<Employees> employees2 = new ArrayList<Employees>();
        readData(employees2);

        assert employees.size() == employees2.size();
        for (int i = 0; i < 1000; i++) {
            Employees e = employees2.get(i);
            assert e.Name.equals("Name" + i);
            assert e.Surname.equals("Surname" + i);
            assert e.ID.equals("" + i);
            assert e.MobNo == 12345678;
        }        
    }

    public static void readData(ArrayList<Employees> listIn) {

        boolean endOfFile = false;
        Employees temp;

        try {

            FileInputStream employeesFile = new FileInputStream("employee.obf");
            ObjectInputStream employeesStream = new ObjectInputStream(employeesFile);
            temp = (Employees) employeesStream.readObject();

            while (endOfFile != true) {
                try {
                    listIn.add(temp);
                    temp = (Employees) employeesStream.readObject();
                } catch (EOFException e) {
                    endOfFile = true;
                }
            }
            employeesStream.close();
        }

        catch (FileNotFoundException e) {
            System.out.println("Employee file was not found");
        } catch (IOException e) {
            System.out.println("Employee file could not be read");
        } catch (ClassNotFoundException e) {
            System.out.println("Employee class was not found");
        }
    }

    public static void writeDate(ArrayList<Employees> employee) {

        try {
            FileOutputStream EmployeesFile = new FileOutputStream("employee.obf");
            ObjectOutputStream EmployeesStream = new ObjectOutputStream(EmployeesFile);

            for (Employees employees : employee) {
                EmployeesStream.writeObject(employees);
            }
            EmployeesStream.close();
        } catch (IOException e) {
            // System.out.println("Error occurred while saving");
            e.printStackTrace();
        }
    }
}