Reputation: 483
So I'm using files to save some scores of my program but the problem is that I don't can't just print them. I tryed several things but I don't find the right one the is my code:
try{
String FILENAME = "FrogScoreFile";
FileInputStream fos = openFileInput(FILENAME);
byte[] b = new byte[1];
fos.read(b);
fos.close();
String score= String b;
game5HighScore.setText(b);
}catch (java.io.FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 4
Views: 17737
Reputation: 13101
try{
String FILENAME = "FrogScoreFile";
FileInputStream fos = openFileInput(FILENAME);
BufferedReader br = new BufferedReader(new InputStreamReader(fos));
String yourText = br.readLine();
br.close();
game5HighScore.setText(yourText);
}catch (java.io.FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
By The way. why not save your score with SharedPreferences Or SQLite database?
public static void saveScore(Context context) {
final SharedPreferences.Editor editor = context.getSharedPreferences(
"settings", Context.MODE_PRIVATE).edit();
editor.putInt("score", 100);
editor.commit();
}
public static int readScore(Context context) {
final SharedPreferences sp = context.getSharedPreferences("settings",
Context.MODE_PRIVATE);
return sp.getInt("score", 0);
}
Upvotes: 0
Reputation: 1045
You can convert Byte array
to string by creating new string
object.
byte[] b = new byte[1];
fos.read(b);
fos.close();
String message = new String(b);
Upvotes: 15