简体   繁体   中英

exception in java: write in a output file

what is:

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) ???

what I try is to write in a output file an object of a class which has like attributes

private static final long serialVersionUID = 1L;
private TreeSet<Product> tree = new TreeSet<Product>();
private Iterator<Product> iterator ;

and this class implements 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();
    }
}

The problem is Iterator<Product> iterator field. Iterator doesn't implement Serializable .

A way to solve this problem would be marking this field as transient .

private transient Iterator<Product> iterator;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM