是什么:
java.io.NotSerializableException: java.util.TreeMap$KeyIterator
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source) ???
我尝试的是在输出文件中写入具有类似属性
的类的对象private static final long serialVersionUID = 1L;
private TreeSet<Product> tree = new TreeSet<Product>();
private Iterator<Product> iterator ;
此类实现Serializable
。
public void scrieArbore() {
try {
ObjectOutputStream outStreamWarehouse = new ObjectOutputStream(new FileOutputStream(new File(WAREHOUSE_FILENAME)));
outStreamWarehouse.writeObject(h);
outStreamWarehouse.flush();
outStreamWarehouse.close();
System.out.println("Safely written warehouse");
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
问题是Iterator<Product> iterator
字段。 Iterator
未实施Serializable
。
解决此问题的方法是将此字段标记为transient
。
private transient Iterator<Product> iterator;