Saving Integer Array

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Saving Integer Array

Postby kdiddy » Fri May 01, 2009 11:35 pm

Hi,

I'm trying to save an integer array but seem to be having an issue just saving a variable. I used the following code based off this thread http://www.anddev.org/write_to_and_read ... t3173.html but it doesn't seem to want to save an integer. I'll finish my level get a high score than save it. But when I reload it, I get a weird number. For example I'll save 2000 but get back 38 when I load it. Sometimes it's 216. It seems really random. Here's my code.

public void WriteSettings(Context context, Integer highscore){
FileOutputStream out = null;


try{
out = openFileOutput("settings.dat",MODE_PRIVATE);
out.write(highscore);
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public int ReadSettings(Context context){
FileInputStream fIn = null;

try{

fIn = openFileInput("settings.dat");
highscorepull = fIn.read();
fIn.close();
Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
finally {
try {
outin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return highscorepull;
}

Ultimately I want to save an array like highscore[0] = highest, highscore[1] = lowest, and so on.

Any ideas as to what classes I should use?

Thanks!
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests