java中的异常:写入输出文件

时间:2014-04-07 19:08:52

标签: java exception treeset

是什么:

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();
    }
}

1 个答案:

答案 0 :(得分:2)

问题是Iterator<Product> iterator字段。 Iterator未实施Serializable

解决此问题的方法是将此字段标记为transient

private transient Iterator<Product> iterator;