gpt4 book ai didi

java - (已解决)序列化(写入)充满对象的Arraylist不断抛出 "NotSerializableException"

转载 作者:行者123 更新时间:2023-12-01 18:36:40 26 4
gpt4 key购买 nike

我是一名 Java 学生,遇到了一些麻烦。所以我正在编写一个名为“LinkedInCLI”的程序它使用 4 个不同的类:

  1. 用户帐户
  2. LinkedInUser
  3. LinkedInException(自定义异常)
  4. LinkedInCLI

我目前正在编写“LinkedInCLI”类来执行各种功能。其中一个函数是将“用户”添加到用户数组列表中,对象 LinkedInUser 由“帐户”的名称、密码和“帐户类型”定义

实际上,它们只是确定有关“用户”信息的所有字符串。它的构造函数如下所示。

public class LinkedInUser extends UserAccount implements  java.io.Serializable ,Comparable<LinkedInUser>{

public LinkedInUser(String user, String pass , String type) {
super(user, pass, type);
}

好的,现在回答这个问题:

一旦我创建了这些用户,他们就会进入 LinkedInUsers 的 ArrayList,如下所示。

private static List<LinkedInUser> users = new ArrayList<>();

它位于 LinkedInCLI 内部,而不是在 main 方法内部。它位于私有(private)领域等旁边。

我想将 ArrayList 序列化为 .DAT 文件,完成此操作后,我想终止我的程序并再次执行它,并从程序执行中读取先前的 ArrayList。完成此操作后,ArrayList 应该与之前的执行相同。

但我一直遇到“NotSerializedException”。我已经编写了 ObjectOutputStream 和 ObjectInputStream,它们看起来编写正确,但有些问题,它不会写入对象。这就是我的流的样子

ObjectOutputStream oos = null;

try
{

// CREATE file path and display it

new File(PATH).mkdirs();


System.out.println(PATH);


// SAVE arraylist
oos = new ObjectOutputStream(new FileOutputStream(PATHPLUSNAME));

oos.writeObject(users);
System.out.println("Writing users to file");
System.out.println(": " + users);



//CLOSE resources
oos.flush();
oos.close();




}
catch (FileNotFoundException FNFO) {
System.out.println("The file was not found");
}
catch(NotSerializableException NSEO) {
System.out.println("Type is not serializable");
}
catch (IOException IOEXO) {
System.out.println("Could not serialize users");
}
<小时/>
 try { 

LinkedInUser test = new LinkedInUser();

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(PATHPLUSNAME));

ArrayList usersCopied = (ArrayList<LinkedInUser>) ois.readObject();



users.addAll(usersCopied);



ois.close();

}
catch (FileNotFoundException FNFI) {
System.out.println("The file was not found");
}
catch (IOException IOEXI) {
System.out.println("Could not Deserialize users");
}
catch (ClassNotFoundException CLNI) {
System.out.println("Could not find class");
}

我的问题确实是:为什么这个异常总是抛出?起初,这只是一个 IOException,但后来我查找了子异常并尝试了所有异常,最后发现它就是“NotSerializedException”。

注意:所有其他类也实现可序列化...

-------------------------------------------------------- --------------------------------这个问题已经解决了,问题是我的 UserAccount 方法实现了可序列化,但它也实现了类似的,它没有实现了可序列化,我也通过查看oracle文档发现了这一点。 UserAccount 类顶部有一些杂散扫描仪,它们也无法序列化。

对于任何帮助我与我一起讨论这个问题的人,谢谢。你们太棒了!

最佳答案

我已经检查了您的代码,没有父类(super class)UserAccount - 它工作正常。

由于您没有提供UserAccount的源代码,那么有两种可能性:

首先是 UserAccount 实现了 java.io.Serialized 确实如您所说。在这种情况下,请确保您的父类(super class)的所有字段(或这些字段的父类(super class)等等......)也实现了 java.io.Serialized

第二 - 事实上您忘记实现 Serialized 或以某种方式无法实现。在这种情况下,要扩展UserAccount,您还需要向该类添加无参数构造函数。换句话说,您的类的第一个不可序列化的父类(super class)需要无参数构造函数。

下表解释得很清楚:

Summary of possible cases and solutions来源:Image

提示:如果它仍然不适合您,那么您可以尝试将 UserAccount 类的字段标记为 transient,然后编译以查看哪个字段导致问题。这些字段将不会被序列化。

transient String field;

关于java - (已解决)序列化(写入)充满对象的Arraylist不断抛出 "NotSerializableException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60024175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com