退出"活动"时,我保存了一些东西。第一个在对象中,第二个是int值。
它可以保存,但当我试图读回时,当我试图获得第二个项目时,我会得到EOFException。
private void saveData(){
FileOutputStream saveFile = null;
try {
saveFile = this.openFileOutput(STATE_SAVE_FILE_NAME, Context.MODE_PRIVATE);
ObjectOutput output = new ObjectOutputStream(saveFile);
output.writeObject(game);
output.write(ScoringForScoreloop.getScore());
output.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
saveFile.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
private void restoreData(){
FileInputStream restoreFile = null;
try {
try {
restoreFile = this.openFileInput(STATE_SAVE_FILE_NAME);
}
catch (FileNotFoundException e) {
inGame = false;
return; // If no file then ok - we will start a new game
}
ObjectInput input = new ObjectInputStream(restoreFile);
game = (GameControl) input.readObject();
ScoringForScoreloop.setScore(input.readInt());
inGame = (game!=null);
} catch (Exception e) {
inGame = false;
return; // If error then we will start a new game
}
finally {
try {
restoreFile.close();
this.deleteFile(STATE_SAVE_FILE_NAME); // delete it so we don't restore from it again
}
catch (Exception ex) {
inGame = false;
return; // If error then we will start a new game
}
}
}
我以前从未在Java中使用过这些类,但我大胆猜测一下。我怀疑这个问题是Java中int和Integer之间的区别。
在编写代码时,您是否尝试过更换
output.write(ScoringForScoreloop.getScore());
带有
output.writeInt(ScoringForScoreloop.getScore());
这样,在类型处理方面,读写应该是对称的。