1

我创建了一个程序,它将计算一些东西并将信息存储到一个哈希表中。我希望它在下次运行时恢复存储在 hashTable 中的先前值并恢复。所以我问如何将对象保存到文件中?

在我的代码中,我使用的是 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)
4

2 回答 2

3

好吧,问题似乎是,您正在尝试序列化不实现 Serializable 的东西。你可以在这里看到:

java.io.NotSerializableException: proj4.QuadraticProbingHashTable$HashEntry

你有两个选择:

  1. 如果可能的话,你让 HashEntry 实现 Serializable
  2. 您将其标记为瞬态,因此它不会被序列化(可能不是您想要的)。

有关更多详细信息,您可以发布课程。

于 2012-05-02T16:30:43.680 回答
2

问题是即使您的表可能是可序列化的,但您已向不可序列化的哈希表添加了键或值。所有对象都可以进入 Hashtable,但并非所有对象都可以序列化。你在你的哈希表中放入了什么样的对象?

于 2012-05-02T16:33:18.550 回答