由於DirectedSparseGraph實現了可序列化(javadoc),爲什麼我不能創建圖形,將其序列化爲文件,然後將其反序列化?調用deserializer.readObject()時會引發「InvalidClassException」,並顯示消息「無效的構造函數」。JUNG的DirectedSparseGraph是否可串行化?
看着javadoc和源文件,它有一個零參數的構造函數。
這是一個庫中的錯誤?或者我錯過了什麼? Java期望什麼類型的構造函數?
DirectedSparseGraph graph = new DirectedSparseGraph();
FileOutputStream underlyingStream = new FileOutputStream("output/temp.jung");
ObjectOutputStream serializer = new ObjectOutputStream(underlyingStream);
serializer.writeObject(graph);
serializer.close();
underlyingStream.close();
FileInputStream underlyingStream2 = new FileInputStream("output/temp.jung");
ObjectInputStream deserializer = new ObjectInputStream(underlyingStream2);
DirectedSparseGraph loadedGraph = (DirectedSparseGraph) deserializer.readObject(); //EXCEPTION THROWN HERE
deserializer.close();
underlyingStream2.close();
例外:
Exception in thread "main" java.io.InvalidClassException: edu.uci.ics.jung.graph.DirectedSparseGraph; edu.uci.ics.jung.graph.DirectedSparseGraph; no valid constructor
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at gui.GraphViewer.main(GraphViewer.java:39)
Caused by: java.io.InvalidClassException: edu.uci.ics.jung.graph.DirectedSparseGraph; no valid constructor
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at gui.GraphViewer.main(GraphViewer.java:33)
您是否打算保存它的頂點,將它們保存在一個文件中,然後將它們從該文件加載到它們的最後狀態?如果是,請檢查'jung-samples-2.0.1.jar'中的'PersistentLayoutDemo.java'(使用它的'jung-samples-2.0.1-sources.jar')。 PersistentLayoutImpl.persist()方法實現PersistentLayout.persist()接口方法,並使用序列化的Point類保存頂點。 – 2012-03-28 01:12:53