Saving a persistent .txt file

Started by
6 comments, last by tmccolgan88 10 years ago

I'm trying to save the player's top score so that It will be available after the process of the game is terminated. It seems like I got my write to work but whenever I try to read the file I get a <code>FileNotFoundException</code>. Wondering if one of you guys can see what I'm doing wrong.


//The method that saves the file.
public void saveGame(int score){
		BufferedWriter outputStream;
		
		try{
			outputStream = new BufferedWriter(new FileWriter(new
					File(getFilesDir()+File.separator+"scores.txt")));
			
			outputStream.write(Integer.toString(score));
			outputStream.close();
			
		} catch (Exception e){
			Log.d("outputStream Exception", e.getMessage());
		}
		
		Log.d("", "saved to file");
	}
	
        //The method that reads the file.
	public int readScores(){
		String read;
		StringBuilder builder = new StringBuilder("");
		BufferedReader buffread;
		
		try {
			buffread = new BufferedReader(new FileReader(new 
					File(getFilesDir()+File.separator+"scores.txt")));
		} catch (FileNotFoundException e1) {
			Log.d("here 0", e1.getMessage());
			return -1;
		}
		

		try {
			while ((read = buffread.readLine()) != null)
				builder.append(read);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		try {
			buffread.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.d("", builder.toString());
		
		return Integer.parseInt(builder.toString());
	}
Advertisement

As it is Android, did you your permissions include the right to modify storage?

As it is Android, did you your permissions include the right to modify storage?

Hey frob. All the documentation that I can find says that when saving to internal storage you don't need to declare permissions. I went ahead and tried the code with and without perms when it wouldn't work though.

When you say you got your save to work I take it you mean you have checked that it is creating the file and writing the score to it?

You can try looking at the Saving Files documentation on the google site for how they suggest saving files.

Or if all you want to do is read and write a few values then I'd suggest using the shared preferences.


SharedPreferences data = getSharedPreferences("LIFETIME_GAME_DATA", Activity.MODE_PRIVATE);
int bestScore = data.getInt("BEST_SCORE", 0);

Save Score:

SharedPreferences data = getSharedPreferences("LIFETIME_GAME_DATA", Activity.MODE_PRIVATE);
data.edit().putInt("BEST_SCORE", score).apply();

Hello TechnoGoth. Ive read the link you posted a few times but it didn't seem to help. The


SharedPreferences

class seems to work perfectly though.

For some reason my code is always returning the fail safe number. I just noticed that it wasn't actually working correctly.


public int readScores(){
			
			SharedPreferences data = getSharedPreferences("LIFETIME_GAME_DATA_2", Activity.MODE_PRIVATE);
			int bestScore = data.getInt("BEST_SCORE", 3);
		    Log.d("Best Score", String.valueOf(bestScore));
			return bestScore;
	
		}
		public void saveGame(int score){
		
			SharedPreferences data = getSharedPreferences("LIFETIME_GAME_DATA_2", Activity.MODE_PRIVATE);
		
			if (score > readScores())		
				data.edit().putInt("BEST_SCORE", score).apply();
		}

This is how my code looks now. I can't seem to figure out what I'm doing wrong.

That all looks fine. I'd check to see if your code is ever reaching the saveGame method.

Yup. My if that called the function in true was wrong.

This topic is closed to new replies.

Advertisement