Reputation: 1
I have a little problem. Hopefully you can help me.
I have to keep the scores of a game in a txt file. At the end of the game, I give it a name (no matter what). first i have to write that name to a txt file with the related score. Now my question is how to write something to a file in the Assets folder. Then I have to show that scores in a text view.
Upvotes: 0
Views: 135
Reputation: 206
I think this can solve your problem
File f = new File(Environment.getExternalStorageDirectory(), "ScoreDetails.txt");
try {
StringBuilder scoreDetails = new StringBuilder();
scoreDetails.append("Score - A");
scoreDetails.append(" \nScore - B");
PrintWriter pw = new PrintWriter(f);
pw.append(scoreDetails.toString());
pw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any confusion.Please let me know
Upvotes: 0
Reputation: 520
You cannot write to the assets folder (see this answer). While you still can write the score to a file (see this answer) I would suggest that you instead write the data pertaining to each game to a database. Database support in Android is great and this would allow for a central store for all the data of all your games. This would make it easier to handle, compare and etc.
Upvotes: 2