Jump to content

  • Log In with Google      Sign In   
  • Create Account


Saving a persistent .txt file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 tmccolgan88   Members   -  Reputation: 253

Like
0Likes
Like

Posted 31 March 2014 - 07:16 PM

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


Sponsor:

#2 frob   Moderators   -  Reputation: 19862

Like
0Likes
Like

Posted 01 April 2014 - 03:11 AM

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


Check out my personal indie blog at bryanwagstaff.com.

#3 tmccolgan88   Members   -  Reputation: 253

Like
0Likes
Like

Posted 01 April 2014 - 06:38 AM

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.



#4 TechnoGoth   Crossbones+   -  Reputation: 2662

Like
2Likes
Like

Posted 01 April 2014 - 01:38 PM

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

Writing Blog: The Aspiring Writer

 

Novels:
Legacy - Black Prince Saga Book One - By Alexander Ballard

Current Projects: Rags to Riches -prototype increment game - Design V1

Android Apps:


#5 tmccolgan88   Members   -  Reputation: 253

Like
0Likes
Like

Posted 05 April 2014 - 02:25 AM

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. 



#6 tmccolgan88   Members   -  Reputation: 253

Like
0Likes
Like

Posted 07 April 2014 - 04:56 PM

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, 07 April 2014 - 04:57 PM.


#7 TechnoGoth   Crossbones+   -  Reputation: 2662

Like
0Likes
Like

Posted 07 April 2014 - 07:48 PM

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


Writing Blog: The Aspiring Writer

 

Novels:
Legacy - Black Prince Saga Book One - By Alexander Ballard

Current Projects: Rags to Riches -prototype increment game - Design V1

Android Apps:


#8 tmccolgan88   Members   -  Reputation: 253

Like
0Likes
Like

Posted 09 April 2014 - 07:40 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS