我搜索了论坛并尝试了不同的方法,但我仍然遇到此错误。会发生的是它只读取输入流中的一个值,然后程序关闭,我得到错误。我不明白为什么它不起作用。
这是代码:
public void readRecords() {
Players records;
try { // input the values from the file
Object obj = null;
while (!(obj = (Players) input.readObject()).equals(null)) { //line 60
records = (Players) obj;
myArr.add(records);
System.out.printf("DEBUG: %-10d%-12s\n", records.getScores(), records.getName());
}
input.close();
} // end try
catch (EOFException endOfFileException) {
return; // end of file was reached
} catch (ClassNotFoundException classNotFoundException) {
System.err.println("Unable to create object.");
} catch (IOException ioException) {
System.err.println("Error during reading from file.");
ioException.printStackTrace();
}
}
这是错误:
Error during reading from file.
java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at hangman.FileReadWriter.readRecords(FileReadWriter.java:60)
at hangman.Game.menu(Game.java:127)
at hangman.Game.findLetterAndPrintIt(Game.java:57)
at hangman.Game.displayMenu(Game.java:31)
at hangman.HangmanTets.main(HangmanTets.java:10)
有人有任何想法吗?