【问题标题】:Saving score to text file isn't updating the file? Android Studio将分数保存到文本文件不会更新文件?安卓工作室
【发布时间】:2016-06-27 06:42:24
【问题描述】:

我正在尝试让我的游戏将分数保存到一个文本文件中,稍后我想从中读取。但是我似乎无法将我的分数实际存储在文件中?

此外,我最终希望能够只存储前 10 个分数,所以如果文件中的分数高于 1,则覆盖分数?解决此问题的最佳方法是什么?

谢谢!

public void saveGame(Integer score){


    try {
        OutputStreamWriter osw = new OutputStreamWriter(getGame().getFileIO().writeFile("storetxt.txt"));
        osw.write(score);
        osw.close();
    }
    catch (IOException e){
        Log.e("Exception", "Couldn't write to file: " + e.toString());
    }
}

【问题讨论】:

    标签: android android-studio outputstream fileoutputstream


    【解决方案1】:

    要存储 10 个分数,我会使用 SharedPreferences。您可以在链接中找到一些代码示例(他们甚至使用高分作为示例)。 通过一些调整,您可以使用它来存储前 10 名分数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      • 2021-05-20
      相关资源
      最近更新 更多