hasherr
hasherr

Reputation: 709

LibGDX writing to text file and receiving various errors

A game I'm currently creating with LibGDX has a 'High Score' feature in which the high score is saved in a text file and then either written over or read to the screen. I'm getting a multitude of errors here. I tried setting permissions to allow for outside file writing, I tried putting my .txt file in different directories. I tried using Gdx.files.internal() and Gdx.files.external() Nothing is working. Here are my two methods for getting and setting the high score.

private int getHighScore()
{
    FileHandle scoreFile = Gdx.files.local("data/high_score.txt");
    String text = scoreFile.readString();
    int highScore = Integer.parseInt(text);
    return highScore;
}

private void setHighScore(int newScore)
{
    FileHandle scoreFile = Gdx.files.local("data/high_score.txt");
    String score = Integer.toString(newScore);
    scoreFile.writeString(score, false);
}

Upon dying in game (when the score is supposed to be shown), I receive this error from LibGDX:

03-06 22:31:37.437: ERROR/AndroidRuntime(14533): FATAL EXCEPTION: GLThread 4617
        com.badlogic.gdx.utils.GdxRuntimeException: File not found: /data/data/hasherr.floppyfish.android.core/files/data/high_score.txt (Local)
        at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:133)
        at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
        at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:198)
        at com.badlogic.gdx.files.FileHandle.readString(FileHandle.java:186)
        at hasherr.ghostly.main.state.DeathState.getHighScore(DeathState.java:122)
        at hasherr.ghostly.main.state.DeathState.render(DeathState.java:87)
        at hasherr.ghostly.main.state.StateManager.render(StateManager.java:37)

Notice here:

File not found: /data/data/hasherr.floppyfish.android.core/files/data/high_score.txt (Local)

/data/data/hasherr.floppyfish.android.core/files/data/high_score.txt is not a directory/file in my game. data/high_score.txt is, and that is the file I'm trying to reach. I'm not very familiar with the Gdx.files.local(path) method, so I'm not sure where it's grabbing this from.

How do I get my program to use this text file without throwing errors?

Upvotes: 0

Views: 4296

Answers (2)

Robert P
Robert P

Reputation: 9783

First of all i would suggest to make sure, the Highscores.txt exists at Gdx.files.local("data/high_score.txt");. To do this you can use: boolean exists = Gdx.files.local("Highscores.txt").exists();. If this boolean is false, instead of reading the .txt you could print "no Highscores". As @MelihYıldız' said the Gdx.files.local point to the private app storage. But if you use it to write and read data you don't need to care about where it is. Just make sure you always use the same FileHandle. To know all different Gdx.files.xxx you could read this. It explains, where it stores data and which apptype has access to it. For Highscores you can also think about using Preferences. A quick tutorial. Note, that Preferences are the only possibility to store data for HTML5 applications. If you want to make your App a Webapp you should deffinitly think about them.

Upvotes: 3

Melih Yıldız'
Melih Yıldız'

Reputation: 425

Gdx uses your applications folder.

From 'File Handling' part of the documentation:

Files can also be stored on the internal storage, where they are readable and writable. Each installed application has a dedicated internal storage directory. This directory is again only accessible by that application. One can think of this storage as a private working area for the application.

And in the chart you can see the local:

Local files are stored relative to the application's root or working directory on desktops and relative to the internal (private) storage of the application on Android. Note that Local and internal are mostly the same on the desktop.

You should use Gdx.files.internal("data/high_score.txt"); instead, if you want to store in devices internal storage.

The reference link: https://github.com/libgdx/libgdx/wiki/File-handling

Upvotes: 0

Related Questions