保存和恢复数据的问题

发布于 2024-11-14 21:50:54 字数 1942 浏览 3 评论 0原文

当我退出活动时,我会保存一些东西。 第一个是对象,第二个是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-11-21 21:50:54

我以前从未在 Java 中使用过这些类,但我大胆猜测一下。我怀疑这个问题是Java中int和Integer之间的区别。

在编写代码中,您是否尝试过替换

output.write(ScoringForScoreloop.getScore());

output.writeInt(ScoringForScoreloop.getScore());

“那样”,在类型处理方面,您应该在读写方面保持对称。

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

output.write(ScoringForScoreloop.getScore());

with

output.writeInt(ScoringForScoreloop.getScore());

That way you should be symmetrical on read and write in terms of type handling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文