Class 不会序列化 java
Class wont serialize java
抱歉,如果有人问过这个问题,但我在序列化我制作的 classes 时遇到了一些问题。我已经测试了字符串和数组列表,但是当我尝试使用由两个字符串组成的 class NoteCard 时,我在序列化和反序列化时出现错误。这些是我在 运行 程序时遇到的错误。
java.io.NotSerializableException: com.leopoldmarx.note.notecard.NoteCard
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.leopoldmarx.note.program.Driver.main(Driver.java:22)
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.leopoldmarx.note.notecard.NoteCard
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.leopoldmarx.note.program.Driver.main(Driver.java:33)
Caused by: java.io.NotSerializableException: com.leopoldmarx.note.notecard.NoteCard
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.leopoldmarx.note.program.Driver.main(Driver.java:22)
错误代码如下:
NoteCard test = new NoteCard("this is the front", "this is the back");
System.out.println(test.getFront() + test.getBack());
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hey.cardset"));
oos.writeObject(test);
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hey.cardset"));
NoteCard n1 = (NoteCard) ois.readObject();
System.out.println(n1.getFront() + n1.getBack());
}
catch(Exception e) {
e.printStackTrace();
}
网上找遍了,翻了几本书也没找到。感谢您的帮助!
确保NoteCard
实现了java.io.Serializable
接口
public class NoteCard implements java.io.Serializable
并且如果 NoteCard
包含其他 class 成员,则确保他们也在实施 java.io.Serializable
.
当你想序列化一个 class 时,总是计算一个 serialVersionUID
,大多数 IDE 都会为你做这件事。如果您不提供,那么java会在您连载时为您提供,但不推荐这样做。
private static final long serialVersionUID = -3457676767677L;
抱歉,如果有人问过这个问题,但我在序列化我制作的 classes 时遇到了一些问题。我已经测试了字符串和数组列表,但是当我尝试使用由两个字符串组成的 class NoteCard 时,我在序列化和反序列化时出现错误。这些是我在 运行 程序时遇到的错误。
java.io.NotSerializableException: com.leopoldmarx.note.notecard.NoteCard
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.leopoldmarx.note.program.Driver.main(Driver.java:22)
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.leopoldmarx.note.notecard.NoteCard
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.leopoldmarx.note.program.Driver.main(Driver.java:33)
Caused by: java.io.NotSerializableException: com.leopoldmarx.note.notecard.NoteCard
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.leopoldmarx.note.program.Driver.main(Driver.java:22)
错误代码如下:
NoteCard test = new NoteCard("this is the front", "this is the back");
System.out.println(test.getFront() + test.getBack());
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("hey.cardset"));
oos.writeObject(test);
oos.close();
}
catch(Exception e) {
e.printStackTrace();
}
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("hey.cardset"));
NoteCard n1 = (NoteCard) ois.readObject();
System.out.println(n1.getFront() + n1.getBack());
}
catch(Exception e) {
e.printStackTrace();
}
网上找遍了,翻了几本书也没找到。感谢您的帮助!
确保NoteCard
实现了java.io.Serializable
接口
public class NoteCard implements java.io.Serializable
并且如果 NoteCard
包含其他 class 成员,则确保他们也在实施 java.io.Serializable
.
当你想序列化一个 class 时,总是计算一个 serialVersionUID
,大多数 IDE 都会为你做这件事。如果您不提供,那么java会在您连载时为您提供,但不推荐这样做。
private static final long serialVersionUID = -3457676767677L;