Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Libgdx写入文件不工作?_Java_Android_File Io_Libgdx_Outputstream - Fatal编程技术网

Java Libgdx写入文件不工作?

Java Libgdx写入文件不工作?,java,android,file-io,libgdx,outputstream,Java,Android,File Io,Libgdx,Outputstream,我正在用Libgdx制作一个游戏,我正在尝试实现一个高分。我的想法是,只要用高分创建一个文件,读取该文件,如果玩家的当前分数高于保存的高分,则覆盖上一个高分,并将当前分数写入该文件。然后,我将读取该文件以显示高分 当我在同一个会话中写入文件并从中读取时,这很好,但是当我关闭应用程序并打开highscore文本文件时,highscore丢失了。因此,基本上,当我写入文件并关闭程序时,文件不会保存我写入的内容。以下是代码- FileHandle highScore; highScore = Gdx.

我正在用Libgdx制作一个游戏,我正在尝试实现一个高分。我的想法是,只要用高分创建一个文件,读取该文件,如果玩家的当前分数高于保存的高分,则覆盖上一个高分,并将当前分数写入该文件。然后,我将读取该文件以显示高分
当我在同一个会话中写入文件并从中读取时,这很好,但是当我关闭应用程序并打开highscore文本文件时,highscore丢失了。因此,基本上,当我写入文件并关闭程序时,文件不会保存我写入的内容。以下是代码-

FileHandle highScore;
highScore = Gdx.files.external("data/highscore.txt");
    OutputStream out = null;
    out = highScore.write(false);
    try {
        out.write(Integer.toString(score).getBytes());
        Gdx.app.log(" score test", highScore.readString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
非常感谢您的帮助。谢谢

阅读并使用共享首选项保存高分

记住使用

prefs.flush()


否则,您的数据将丢失在设备上。

为什么不使用共享首选项保存高分???共享首选项是什么意思?链接到图坦卡蒙会很有帮助,谢谢!