我创建了一个程序来计算某些东西并将信息存储到hashTable中。我想要它做的是下次它运行以恢复存储在hashTable和resume中的先前值。所以我问如何将对象保存到文件中?
在我的代码中,我使用的是FileInputStream和FileOutputStream。到目前为止,这是我的代码的一小段内容:
@SuppressWarnings("unchecked")
private void obtainSaveFile(){
String file = new String("./saveFile.txt");
try{
//Create the file if it doesn't exist
if( !( (new File(file)).exists() ) ){
//Create new empty file
(new File(file)).createNewFile();
//Creates a fresh new hashTable
hashTable = new QuadraticProbingHashTable<TicTacToeBoard>();
}
else{
//Obtain the hashTables saved config.txt file
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
//Obtain the previously saved hashTable
//-----------------------------------
//This line of code gives me an error
//-----------------------------------
hashTable = (QuadraticProbingHashTable<TicTacToeBoard>)ois.readObject();
ois.close();
}
//Create save file configs.txt
FileOutputStream fos = new FileOutputStream(file);
saveFile = new ObjectOutputStream(fos);
}catch(Exception e){
e.printStackTrace();
//Creates a fresh new hashTable
//This line of code gives me an error
hashTable = new QuadraticProbingHashTable<TicTacToeBoard>();
}
}
最初,当没有文件时,它运行正常。但在那之后,它一直给我错误。当我从文件中取出一个对象并将其转换为QuadraticProbingHashTable并将其设置为hashTable时,它并不喜欢。
此外,这里输出的是错误:
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: proj4.QuadraticProbingHashTable$HashEntry
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at proj4.TicTacToeSolver.obtainSaveFile(TicTacToeSolver.java:308)
at proj4.TicTacToeSolver.solve(TicTacToeSolver.java:120)
at proj4.Main.main(Main.java:41)
Caused by: java.io.NotSerializableException: proj4.QuadraticProbingHashTable$HashEntry
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeArray(Unknown Source)
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)
at proj4.TicTacToeSolver.solve(TicTacToeSolver.java:246)
... 1 more
Exception in thread "main" java.lang.NullPointerException
at proj4.TicTacToeSolver.solve(TicTacToeSolver.java:246)
at proj4.Main.main(Main.java:41)
答案 0 :(得分:3)
嗯,问题似乎是,你正在尝试序列化一些没有实现Serializable的东西。 你可以在这里看到:
java.io.NotSerializableException: proj4.QuadraticProbingHashTable$HashEntry
您有两种选择:
有关详细信息,您可以发布课程。
答案 1 :(得分:2)
问题在于,即使您的表可能是可序列化的,您也可以向哈希表添加一个键或值,该键不可序列化。所有对象都可以进入Hashtable,但并非所有对象都可以序列化。你在哈希表中放入了什么样的对象?