Search code examples
androidandroid-studiooutputstreamfileoutputstream

Saving score to text file isn't updating the file? Android Studio


I'm trying to get my game to save the scores to a text file, which I later want to read from. However I cannot seem to get my scores to actually be stored on the file?

Also I eventually would like to be able to only store the top 10 scores, so overwrite a score if it is higher than one in the file? What would be the best means of going about this?

Thanks!

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

Solution

  • To store 10 scores I would use SharedPreferences. You can find some code examples in the link (they even use high-score as the example). With some adjustments you can use it to store top 10 scores.