【问题标题】:Android: Unable to save more than the number 195 in a fileAndroid:无法在文件中保存超过数字 195
【发布时间】:2016-01-10 10:16:27
【问题描述】:

对于我的游戏,我将用户玩过的游戏数量存储在内部存储的文件中。这可以完美运行,直到数字 195 被存储之后,无论玩了多少游戏,它都不会增加。

public void saveNumGames(){
    Log.d("saveNumGames", "Called");
    int numGames;
    File numGamesFile = new File(mainActivity.getFilesDir(), "numGames");
    try {

        BufferedInputStream BIS = new BufferedInputStream(new FileInputStream(numGamesFile));
        numGames = BIS.read();
    }catch (Exception e) {
        e.printStackTrace();
        numGames = 0;
    }
    try{
        numGames++;
        Log.d("numGames", Integer.toString(numGames));
        FileOutputStream fos = new FileOutputStream(numGamesFile);
        PrintWriter PW = new PrintWriter(new OutputStreamWriter(fos));
        PW.write(numGames);
        PW.flush();
        PW.close();
        if(numGames==12){
            Games.Achievements.unlock(MainActivity.apiClient, "CgkIlcXhyp4YEAIQDA");
        } else if(numGames==50){
            Games.Achievements.unlock(MainActivity.apiClient, "CgkIlcXhyp4YEAIQDQ");
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

我的想法是,这是因为数字 195 是一个字节(或位?)中可存储的最大值。如果是这样,我应该怎么做才能防止出现这个数字上限。

【问题讨论】:

    标签: java android file byte storage


    【解决方案1】:

    首先你用错了两个方法。第一个是 numGames = BIS.read();方法 read() 设计为只读一个字节。另一种方法 PW.write(numGames) 方法 write 哪个参数是 int 指定要写入的字符。 它应该工作:

      public static void saveNumGames() {
        Log.d("saveNumGames", "Called");
        int numGames;
        File numGamesFile = new File(mainActivity.getFilesDir(), "numGames");
        try {
            numGames = new Integer(new String(Files.readAllBytes(Paths.get(mainActivity.getFilesDir() + "numGames"))));
        } catch (Exception e) {
            e.printStackTrace();
            numGames = 0;
        }
    
        try (FileOutputStream fos = new FileOutputStream(numGamesFile)) {
            numGames++;
            Log.d("numGames", Integer.toString(numGames));
            PrintWriter PW = new PrintWriter(fos);
            PW.write(String.valueOf(numGames));
            PW.flush();
            PW.close();
            if (numGames == 12) {
                Games.Achievements.unlock(MainActivity.apiClient, "CgkIlcXhyp4YEAIQDA");
            } else if (numGames == 50) {
                Games.Achievements.unlock(MainActivity.apiClient, "CgkIlcXhyp4YEAIQDQ");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      【解决方案2】:

      对于您想要实现的目标,我会使用 SharedPrefereces 类 (SharedPreferences)

      Saving key-value 是这个的示例用法

      【讨论】:

        【解决方案3】:

        一个字节是 8 位,所以你肯定需要更多的东西来存储像 195 这样的东西。当 196 存储在文件中时,我想知道以下内容。

        这一行之后numGames的值是多少

        numGames = BIS.read();

        这会打印什么

        Log.d("numGames", Integer.toString(numGames));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-09-11
          • 1970-01-01
          • 2016-02-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-18
          • 1970-01-01
          相关资源
          最近更新 更多