无法使用fileio功能打开Object Arraylist

时间:2014-10-28 08:56:00

标签: java file-io arraylist

我正在尝试使用函数来打开带有fileIO的对象的arraylist但是现在我被卡住了。我认为我的setter函数有问题。 (文件test.txt已经存在)

我正在使用以下类来使fileio函数正常工作

Paslijst

public class Paslijst implements Serializable {
    private ArrayList<Pas> paslijst;

    public ArrayList<Pas> setPaslijst(ArrayList<Pas> paslijst){
    this.paslijst = paslijst;
    return paslijst;
 }

FileIOPas

// This function opens a file
public Paslijst openen(String filenaam) 
    throws IOException, ClassNotFoundException {
    FileInputStream fileInputStream = new FileInputStream(filenaam);
    ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); 
    return (Paslijst) objectInputStream.readObject();
}

主要

 public static void main(String[] args) {

 FileIOPas fileiopas = new FileIOPas();
 Paslijst paslijst = new Paslijst();

 try {
       paslijst.setPaslijst(fileiopas.openen("test.txt"));
      } 
      catch (IOException e) {
          System.out.println(" IO openen mislukt, want: " + e.toString());
        } 
      catch (ClassNotFoundException e) {
          System.out.println("class not found: " + e.toString());
        }
     }

1 个答案:

答案 0 :(得分:1)

你在paslijst.setPaslijst()中传递了错误的参数。我已经编辑了你的课程,这可能会有所帮助。

public class Main {

    public static void main(String[] args) {
         FileIOPas fileiopas = new FileIOPas();
         Paslijst paslijst = new Paslijst();
         paslijst.setPaslijst(fileiopas.openen("test.txt"));
    }
}

public class FileIOPas {

    public ArrayList<Pas> openen(String filenaam) {

        try (FileInputStream fileInputStream = new FileInputStream(filenaam);
                ObjectInputStream objectInputStream = new ObjectInputStream(
                        fileInputStream);) {
            return (ArrayList<Pas>) objectInputStream.readObject();
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        return null;
    }

}

public class Paslijst implements Serializable {

    private ArrayList<Pas> paslijst;

    public ArrayList<Pas> getPaslijst() {
        return paslijst;
    }

    public void setPaslijst(ArrayList<Pas> paslijst) {
        this.paslijst = paslijst;
    }

}