2014-12-03 62 views
0

我使用ConcurrentRadixTree並將大於1百萬個單詞放入它中。 這棵樹的創建需要很長時間,所以我想創建我的樹只有一次,並將其另存爲一個文件 - >所以我可以加載我的樹從文件下次使用,而不是再次創建它(應該更快) 。將RadixTree另存爲文件

File file = new File(myPath); 
FileOutputStream f = new FileOutputStream(file); 
ObjectOutputStream s = new ObjectOutputStream(f); 
s.writeObject(myTree); 
s.close(); 

但是,當我嘗試存儲,我得到 「NotSerializableException」:

Exception in thread "main" java.io.NotSerializableException: com.googlecode.concurrenttrees.radix.ConcurrentRadixTree 
at java.io.ObjectOutputStream.writeObject0(Unknown Source) 
at java.io.ObjectOutputStream.writeObject(Unknown Source) 
at ForTests.main(Test.java:29) 

任何想法如何存儲RadixTree?你得到你的異常

回答

0

原因是您正在使用的對象是不可序列

我能想到的是

最簡單的解決方案來擴展你的樹的實現:

public class YourSerializableClass extends ConcurrentRadixTree implements Serializable 

或者你可以實現自己的readObject writeObject方法。更多的信息在這裏:

https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html