Jump to content
  • Advertisement
Sign in to follow this  
tmccolgan88

Saving a persistent .txt file

This topic is 2018 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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

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

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. 

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!