我有一个非常大的项目作为作业(针对 Uni 的 OOP 课程):一个学校登记册,学生可以在其中查看他们的成绩,教师可以添加成绩等等。
“基”类是一个单例,它包含所有使用的类(Java),例如一组用户、类(如学校类)和一个将类和教师与课程相关联的 TreeMap。
我想序列化这个基类(Central),以保存修改后的数据。问题是我得到了这个异常
java.io.NotSerializableException: liceu.Central$1
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:440)
at java.util.TreeMap.writeObject(TreeMap.java:2265)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1495)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at liceu.Main.main(Main.java:31)
我所有的类都实现了可序列化,并且它们没有瞬态或静态字段(除了单例,它具有实例变量和 getInstance 方法作为静态)。
因为要发布很多代码(而且我会冒着在提交之前发布它来使我的任务无效的风险)我试图通过尝试隔离错误来进行概念验证。
public class Central implements Serializable
{
private ArrayList <User> users;
private ArrayList <Class> classess;
private TreeMap <Course, TreeMap <Class, Professor>> reunite;
private static Central instance = null;
private Central()
{
users = new ArrayList<>();
classess = new ArrayList<>();
reunite = new TreeMap<>(new Comparator<Student>(){
@Override
public int compare(Student e1, Student e2)
{
return e1.getName().compareTo(e2.getName());
}
});
}
}
如果我只保留前 2 个 ArrayList,则序列化过程有效。问题出在 TreeMap 上。
TreeMap 类是否可序列化?(一般)是因为匿名比较器吗?
这是带有序列化的主类
public class Main
{
public static void main(String args[])
{
Central cent = Central.getInstance();
FileOutputStream fos;
ObjectOutputStream oos;
cent.addUser(new Student(3,"id","pass","name","surname"));
cent.addUser(new Student(3,"id2","pass","name","surname"));
cent.addUser(new Student(3,"id1","pass","name","surname"));
try
{
fos = new FileOutputStream("save.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(cent);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}