保存和恢复数据的问题
当我退出活动时,我会保存一些东西。 第一个是对象,第二个是 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
}
}
}
I am saving a couple of things when I exit an Activity.
First in an object and the second is an int value.
It saves ok but when I try to read back I get an EOFException when trying to get the second item.
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
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前从未在 Java 中使用过这些类,但我大胆猜测一下。我怀疑这个问题是Java中int和Integer之间的区别。
在编写代码中,您是否尝试过替换
为
“那样”,在类型处理方面,您应该在读写方面保持对称。
I've never used these classes in Java before, but I'll hazard a guess. I suspect the problem is a difference between int and Integer in Java.
In the write code, have you tried replacing
with
That way you should be symmetrical on read and write in terms of type handling.