gpt4 book ai didi

java - 尝试从数组写入对象时出现不可序列化异常

转载 作者:行者123 更新时间:2023-11-30 04:25:27 25 4
gpt4 key购买 nike

我在运行应用程序文件并尝试将对象写入 .dat 文件时遇到 NotSerializedException。

    try{
Person[] personList = new Person[3];
personList[0] = new TeamLeader(8, "Mike Johnson", 29);
personList[1] = new Engineer(0.4,"Russell Sanders", 22);
personList[2] = new Engineer(0.7, "Steven Peterson", 25);
out = new ObjectOutputStream(new FileOutputStream("persons.dat"));
for(int i = 0; i < personList.length; i++) {
out.writeObject(personList[i]);//error line 85
}
//out.writeObject(personList);
}

catch(IOException e) {
System.out.println("Problem writing file");
e.printStackTrace();
}

错误

java.io.NotSerializableException: TeamLeader
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java: 346)
at CharlieBrownP6.main(CharlieBrownP6.java: 85)

错误继续到由于相同的错误而中止写入

最佳答案

调用 writeObject 的对象必须实现 Serialized 接口(interface) ( reference )。

关于java - 尝试从数组写入对象时出现不可序列化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15984345/

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