Sign in to follow this  
tmccolgan88

Saving a persistent .txt file

Recommended Posts

tmccolgan88    255

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

Share this post


Link to post
Share on other sites
tmccolgan88    255

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.

Share this post


Link to post
Share on other sites
TechnoGoth    2937

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

Share this post


Link to post
Share on other sites
tmccolgan88    255

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.

Edited by tmccolgan88

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this