[英]Problems with saving and restoring data
当我退出活动时,我正在保存一些东西。 第一个在 object 中,第二个是 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());
这样,就类型处理而言,您应该在读写方面是对称的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.