Accepted answer

i'm only a first year student in java programming and i am a new user here at, so pardon me if coding for android has some special rules i don't know about, which prevents this simple and humble example from working. but here is how i would read from a file in the simplest of ways.

file tempfile = new file("<subdirectoryifany/name_of_file.txt");

scanner readfile = new scanner( tempfile );

// assuming that you can structure the file as you please with fx each bit of info
// on a new line.

int counter = 0;
while ( readfile.hasnextline() ) {
    score[counter] = readfile.nextline();

as for the writing back to the file? put it in an entirely different method and simply make a simplified tostring-like method, that prints out all the values the exact way you want them in the file, then create a "loadtofile" like method and use the to string method to print back into the file with a printstream, something like below.

file tempfile = new file("<subdirectoryifany/name_of_file.txt");

printstream write = new printstream(tempfile);

// specify code for your particular program so that the tostring method gets the 
// info from the string array or something like that.

write.print( <objectname/this>.tostringlikemethod() );
// remember the /n /n in the tostringlikemethod so it prints properly in the file.

again if this is something you already know, which is just not possible in this context please ignore me, but if not i hope it was useful. as for the exceptions, you can figure that you yourself. ;)


since you are a beginner, and i assume you are trying to get things off the ground as quickly as possible, i'd recommend using sharedpreferences. basically it is just a huge persistent map for you to use! having said that... you should really learn about all the ways of storage in android, so check out this document:

the android docs are awesome! fyi sharedpreferences may not be the best and awesomest way to do this... but i'm all for quick prototyping as a learner. if you want, write a wrapper class around sharedpreferences.

Related Query

More Query from same tag