Java中的TreeMap Map.Entry NotSerializableException

时间:2018-03-14 23:42:20

标签: java serialization treemap objectoutputstream

我正在尝试将TreeMap序列化为文本文件,但我无法让它工作。我尝试过打印/调试和阅读Javadocs。

My Record和Table类都实现了serializable,我还构建了一个自定义比较器,它也为我的新TreeMap<>()实现了serializable。

我相信我已将问题缩小到:

  • 我正在尝试序列化Map.Entry,它可能不是可序列化的,因此也就是NotSerializableExceptions。
  • TreeMap是/不可序列化的(?)。
  • (未知来源) - 不确定未知来源对我的writeObject java.io.ObjectInputStream.readObject0(未知来源)和 java.io.ObjectInputStream.writeObject0(未知来源)

我的主要目标是通过在writeObject()时正确地序列化Map.Entry或我的TreeMap来消除NotSerializableExceptions。

第二个问题是' readObject0(未知来源)'是。在其他类似我的问题的情况下,有一个ObjectOutputStream:1356,我的未知来源是。

我目前正在使用Java 8.

class Record实现Serializable {

private String[] row;

类表实现了Serializable {

private TreeMap<String, Record> tm = new TreeMap<>(
        new ReverseNaturalOrderComparator());

private String[] columns;
private int totalRecords;
[...]
[...]
[...]
void insertRecord( String key, Record r ) { 
    if( r.rowArrayLength() == numRowFields ) {
        tm.put(key, r); 
        totalRecords++;
    }
    else { 
        System.out.println("Number of fields do not match. " + 
        "Please enter a record with " + numRowFields + " fields");
    }
}

class Files {

File file = new File("KeyValues.txt");

void writeFile(Table t) throws IOException {

    TreeMap<String, Record> ftm = t.getTreeMap();
    FileOutputStream os = new FileOutputStream(file);       
    ObjectOutputStream oos = new ObjectOutputStream(os);

    Set set = ftm.entrySet();
    Iterator i = set.iterator();
    while(i.hasNext()) {

        Map.Entry<String, Record> keyValue = (Map.Entry<String, Record>)i.next();

        if( !file.exists() ) { System.out.println("No such file exists"); }
        else { oos.writeObject(keyValue); } // <---causing error
    }
    oos.close();
    os.close();

}

类Files中的以下代码块导致错误:     Iterator i = set.iterator();     while(i.hasNext()){

    Map.Entry<String, Record> keyValue = (Map.Entry<String, Record>)i.next();

    if( !file.exists() ) { System.out.println("No such file exists"); }
    else { oos.writeObject(keyValue); } // <---causing error
}
oos.close();
os.close();

} main():

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
    Table t = new Table();

    Record test0 = new Record("We are the champions, my friend.");
    Record test1 = new Record("hope this works, but might not");
    Record test2 = new Record("sweet, yellow bananas");
    Record test3 = new Record("and bad mistakes Ihave made afew");
    Record test4 = new Record("Now using and printing a TreeMap<>");
    Record test5 = new Record("Exploring Antarctica alongside the polar bears");    

    t.insertRecord("3",test3);
    t.insertRecord("0",test0);
    t.insertRecord("2",test2);
    t.insertRecord("4",test4);
    t.insertRecord("1",test1);
    t.insertRecord("5",test5);

    try { writeFile(t); } //this is giving me the error.

错误记录

java.io.NotSerializableException: java.util.TreeMap$Entry
Final filepath : ...KeyValues.txt
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Files.writeFile(Files.java:52)
    at Files.test(Files.java:87)
    at Files.run(Files.java:64)
    at Files.main(Files.java:62)
java.io.WriteAbortedException: writing aborted; 
java.io.NotSerializableException: java.util.TreeMap$Entry
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Table.readFile(Table.java:92)
    at Files.test(Files.java:100)
    at Files.run(Files.java:64)
    at Files.main(Files.java:62)
Caused by: java.io.NotSerializableException: java.util.TreeMap$Entry
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Files.writeFile(Files.java:33)
    at Files.test(Files.java:66)
    at Files.run(Files.java:48)
    at Files.main(Files.java:46)

1 个答案:

答案 0 :(得分:0)

如果要序列化的类包含对象的引用,则描述这些对象的类也应该实现Serializable(在本例中为Record类)。

修改:您也可以尝试将 1000014 function calls in 13.193 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 13.193 13.193 {built-in method builtins.exec} 1 0.000 0.000 13.193 13.193 <string>:1(<module>) 1 0.000 0.000 13.193 13.193 run.py:138(fun) 1 7.948 7.948 13.193 13.193 test.py:4(rbf_network) 1000000 5.245 0.000 5.245 0.000 {built-in method math.exp} 1 0.000 0.000 0.000 0.000 run.py:141(<listcomp>) 6 0.000 0.000 0.000 0.000 {method 'rand' of 'mtrand.RandomState' objects} 1 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.array} 1 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.zeros} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} Time without CProfile: 4.139716863632202 更改为File file = new File("KeyValues.txt");