简体   繁体   English

Libgdx写入文件不工作?

[英]Libgdx Writing to File not Working?

Im making a game in Libgdx, and I'm trying to implement a highscore. 我正在Libgdx做一个游戏,我正试图实现一个高分。 My thought process was to just make a file with the highscore, read the file, and if the player's current score is higher than the saved highscore, override the previous highscore and write the current one to the file. 我的思维过程是只用高分数制作一个文件,读取文件,如果玩家的当前分数高于保存的高分,则覆盖之前的高分并将当前分数写入文件。 Then, I would read the file to display the highscore. 然后,我会读取文件以显示高分。
When I write to the file and read from it while in the same session it's fine, but when I close out the application and open the highscore text file, the highscore is missing. 当我在同一个会话中写入文件并从中读取时,它很好,但当我关闭应用程序并打开高分文本文件时,缺少高分。 So basically, when I write to the file and close out the program, the file doesn't save whatever I wrote to it. 所以基本上,当我写入文件并关闭程序时,该文件不保存我写给它的任何内容。 Here's the code- 这是代码 -

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();
    }

Help would be much appreciated. 非常感谢帮助。 Thanks! 谢谢!

read this link and use shared preference for saving highscore . 阅读此链接并使用共享首选项来保存高分。

and do remember to use 并记得使用

prefs.flush();

else your data will be lost on devices. 否则您的数据将丢失在设备上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM