I have a simple game in which I would need to store a highscore (float) to a file and then read it next time the user goes on the app. I want it to be saved on the device, however, I have found no way to do that. How can I persist data on the device to a chosen location ?
-
1Voted down in less than 10 seconds? – Tasty Table Sep 06 '15 at 12:40
-
I'm not the downvoter, but I can see why. Never say "I'm a noob", or "Sorry for my english" and this kind of things in a question. It adds no information – Dici Sep 06 '15 at 12:41
-
I edited your question with the minimum, relevant information (to my opinion) – Dici Sep 06 '15 at 12:44
-
OK, thanks for the info. – Tasty Table Sep 06 '15 at 12:44
-
So which part are you having problems with? Writing to a file, reading from a file, serializing / deserializing floats? Did you read [this document](http://developer.android.com/training/basics/data-storage/files.html)? – Tobias Sep 06 '15 at 12:45
-
I can't seem to even make the file. I read the document but it just gave me an "Unknown entity FileOutputStream" – Tasty Table Sep 06 '15 at 12:53
-
1You can google for `save a file android java` and get everything you could possibly need. You are getting downvoted because you put forth 0 effort to solve this on your own. – takendarkk Sep 06 '15 at 12:53
-
If you are writing code, put it in your question. As it stands right now it looks like have done nothing. – takendarkk Sep 06 '15 at 12:53
-
I've spent ages googling on my own. – Tasty Table Sep 06 '15 at 12:54
-
Simply use the [SharedPreferences](http://developer.android.com/intl/es/training/basics/data-storage/shared-preferences.html). Use `putFloat()` and `getFloat()` to save and retrieve your float value, respectively. – Phantômaxx Sep 06 '15 at 13:10
-
Frank, I tried that too. I just got an error: "getSharedPreferences is undefined" – Tasty Table Sep 06 '15 at 13:13
-
If so, you messed something up. It works; believe me. – Phantômaxx Sep 06 '15 at 13:33
-
https://github.com/libgdx/libgdx/wiki/Preferences – Xoppa Sep 06 '15 at 19:54
3 Answers
You can use internal storage. This will create files that can be written to and read from. The best way to do this is to create a separate class that handles files. Here are two methods that will read and write the highscore.
To set the highscore, use setHighScore(float f).
public void setHighScore(float highscore){
FileOutputStream outputStream = null;
try {
outputStream = (this).openFileOutput("highscore", Context.MODE_PRIVATE);
outputStream.write(Float.toString(f)).getBytes());
outputStream.close();
} catch (Exception e) {e.printStackTrace();}
}
To get the highscore, use getHighScore().
public float getHighScore(){
ArrayList<String> text = new ArrayList<String>();
FileInputStream inputStream;
try {
inputStream = (this).openFileInput("highscore");
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
text.add(line);
}
bufferedReader.close();
} catch (Exception e) { e.printStackTrace();}
return Float.parseFloat(text.get(1));
}

- 183
- 1
- 11
-
In my Files class I get an error: "Unknown method isEmpty of 'java.lang.string[]'" – Tasty Table Sep 06 '15 at 18:56
-
I still get an error. See my picture that I put showing the errors http://i.stack.imgur.com/mEubi.png – Tasty Table Sep 07 '15 at 14:56
-
@TastyTable I changed the answer completely. Put these two methods in your Activity class. – Arjun Panickssery Sep 07 '15 at 15:33
-
Thanks for your help, but I get another error: 'Unknown method "openFileOutput"' :( – Tasty Table Sep 07 '15 at 18:36
-
@TastyTable You are doing this in an activity, correct? You seem to be having a problem getting the context of the activity. Could you post your full code? – Arjun Panickssery Sep 07 '15 at 19:29
-
@TastyTable And you're welcome. This will be solved soon, and then I can get my green checkmark. – Arjun Panickssery Sep 07 '15 at 19:30
-
-
Using File Handling - I would suggest going basic by using a file handling technique. Auto create a text file using the java's InputStream and OutputStream classes. Then add the float inside it. As suggested in the comments above too.
Using properties files - Refer this code - http://www.mkyong.com/java/java-properties-file-examples/
Using a database - You can go ahead and use a database which will safely store the score and make sure no one tampers with it easily. Tutorial for this - http://www.tutorialspoint.com/jdbc/

- 921
- 13
- 19
Try simple with very few lines.
public void saveHighScore(File file, float highScore){
try{
Formatter out = new Formatter(file);
out.format("%f", highScore);
out.close();
}catch(IOException ioe){}
}

- 2,499
- 1
- 11
- 22