Sunday 15 August 2010

java - Save/Load a file in android for a game -



java - Save/Load a file in android for a game -

i have created game in android. have written class input/ouput prefer install location external. want create basic questions. first of file utilize .txt (i know not best way save info utilize testing). unusual part when the game on should automatically save user highscores not, when close app , restart highscores have disappeared. larn prefered file type saving settings/highscores/coins etc (hopefully secured) is. lastly debug game using nexus 5 whitch not have external storage (it should stored locally though). code, in advance :).

public class androidfileio implements fileio { context context; assetmanager assets; string externalstoragepath; public androidfileio(context context) { this.context = context; this.assets = context.getassets(); this.externalstoragepath = environment.getexternalstoragedirectory() .getabsolutepath() + file.separator; } public inputstream readasset(string filename) throws ioexception { homecoming assets.open(filename); } public inputstream readfile(string filename) throws ioexception { homecoming new fileinputstream(externalstoragepath + filename); } public outputstream writefile(string filename) throws ioexception { homecoming new fileoutputstream(externalstoragepath + filename); } public sharedpreferences getpreferences() { homecoming preferencemanager.getdefaultsharedpreferences(context); } }

my game class has method

public fileio getfileio() { homecoming fileio; }

this way load file

settings.load(game.getfileio());

and finaly save/load methods of settings class

public static void load(fileio files) { bufferedreader in = null; seek { in = new bufferedreader(new inputstreamreader( files.readfile("mrnom.txt"))); soundenabled = boolean.parseboolean(in.readline()); (int = 0; < 5; i++) { highscores[i] = integer.parseint(in.readline()); } } grab (ioexception e) { // :( it's ok have defaults } grab (numberformatexception e) { // :/ it's ok, defaults save our day } { seek { if (in != null) in.close(); } grab (ioexception e) { } } } public static void save(fileio files) { bufferedwriter out = null; seek { out = new bufferedwriter(new outputstreamwriter( files.writefile("mrnom.txt"))); out.write(boolean.tostring(soundenabled)); (int = 0; < 5; i++) { out.write(integer.tostring(highscores[i])); } } grab (ioexception e) { } { seek { if (out != null) out.close(); } grab (ioexception e) { } } }

here save called

private void updategameover(list<touchevent> touchevents) { int len = touchevents.size(); for(int = 0; < len; i++) { touchevent event = touchevents.get(i); if(event.type == touchevent.touch_up) { if(event.x >= 128 && event.x <= 192 && event.y >= 200 && event.y <= 264) { if(settings.soundenabled) assets.click.play(1); //debug begin fileio fileio = game.getfileio(); settings.save(fileio); //debug end game.setscreen(new mainmenuscreen(game)); return; } } } }

your issue in save method when write strings out reference. not saving value per line, later reading value per line in load method. current code save next in mrnom.txt file: true10203040 instead of true\n10\n20\n30\n40.

to prepare this, 1 way change:

out.write(boolean.tostring(soundenabled));

to

out.write(boolean.tostring(soundenabled) + "\n");

and

out.write(integer.tostring(highscores[i]));

to

out.write(integer.tostring(highscores[i]) + "\n");

java android fileinputstream fileoutputstream

No comments:

Post a Comment