书写中止;java.io.NotSerializableException

书写中止;java.io.NotSerializableException,java,Java,我正在尝试读取文件中的所有对象。下面是用于从文件中获取对象列表的方法的片段 public List displayParties() { ObjectInputStream ois = null; List<RegisterParty> results = new ArrayList(); try { FileInputStream fis = new FileInputStream("/media/user/d

我正在尝试读取文件中的所有对象。下面是用于从文件中获取对象列表的方法的片段

public List displayParties() {
        ObjectInputStream ois = null;
        List<RegisterParty> results = new ArrayList();
        try {
            FileInputStream fis = new FileInputStream("/media/user/disk2/myapp/assignment/party.ser");
            ois = new ObjectInputStream(fis);
            while (true) {
                System.out.println(" inside while true");
                results.add((RegisterParty) ois.readObject());
                System.out.println(results);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        catch (Exception ex){
                ex.printStackTrace();

        } finally{
            try {
                ois.close();
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
        return results;
    }
但得到以下例外情况:

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: models.RegisterParty
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at helper.HelperFunctions.displayParties(HelperFunctions.java:39)
    at services.PartyOperations.listAllParties(PartyOperations.java:64)
    at assignment.App.main(App.java:34)
    at helper.HelperFunctions.saveParty(HelperFunctions.java:24)
    at services.PartyOperations.registerParty(PartyOperations.java:53)
    at assignment.App.main(App.java:28)
Caused by: java.io.NotSerializableException: models.RegisterParty
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
    at helper.HelperFunctions.saveParty(HelperFunctions.java:20)
    at services.PartyOperations.registerParty(PartyOperations.java:52)
    ... 1 more
RegisterParty实现可序列化接口。它是一个基于菜单的应用程序,所以当我将对象保存在文件中时,它的保存成功。但调用该方法来获取列表中的所有对象,它会抛出异常。知道为什么吗

此方法已成功执行以序列化对象:

public void saveParty(RegisterParty registerParty){
    try {
        FileOutputStream fout = new FileOutputStream("/media/user/disk2/myapp/assignment/party.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fout);
        oos.writeObject(registerParty);
        oos.close();
        System.out.println("Successfull Register");
        System.out.println("========Select you choice========");
        App.main(str);
    }
    catch (Exception ex){
            ex.printStackTrace();
    }
}

在实现可序列化对象时,我将静态变量serialVersionUID添加到类中:

private static final long serialVersionUID = -1892561327013038124L;
这一行由eclipse或android studio作为建议添加。我不知道如何使用IntelliJIdea


值得一试!我相信此ID用于反序列化。

文件读取和文件写入中存在冲突

    FileInputStream fis = new FileInputStream("/media/user/disk2/myapp/assignment/party.ser");
    FileOutputStream fout = new FileOutputStream("/media/user/disk2/myapp/assignment/party.txt");

您也可以共享RegisterParty类代码吗?如何获取该文件?您使用的是哪个JDK版本?
而(true){
有点奇怪。您打算如何退出循环?以及写入该文件的代码?不,不是,此id用于跟踪类的版本以进行串行和反序列化(如果反序列化错误的版本,则不希望内存中有损坏的副本)。甚至不需要它,因为如果没有提供任何版本,编译器将创建默认副本。。。
    FileInputStream fis = new FileInputStream("/media/user/disk2/myapp/assignment/party.ser");
    FileOutputStream fout = new FileOutputStream("/media/user/disk2/myapp/assignment/party.txt");