0

I'm developing a little Game at the Moment (Android-App) and I want to save my Highscores into a .txt-File, because if I would save it just in a list, the list would be empty everytime I start the App new. For java-programs in general this is not a problem for me, because the FileWriter creates a .txt in the program-folder. If I do that in Android, I get (of course) the error, that he couldnt create the file.

So how do I tell the FileWriter where to save the File and where should I save it? Does the FileWriter deletes an existing file and creates a new file with the same name if the filename already exists like the "MODE_PRIVATE"-Mode from the FileOutput?

I know I could use the FileOutput/InputStream to save a file in the internal storage, but the FileOutput just accepts bytes (which is no problem with the .getBytes()-Method), but I cant find a way to transform the bytes back into Strings when I want to read them.

(I apologize for any grammar-mistakes as english is not my native language)

UPDATE:

At first big thanks for the help so far. But i still need some specific help. Thats what i want to do: Everytime my Game is finished, I switch to the Points activity and give him the points reached. In the points-activity I put the points into a List and write this list into a file in the internal storage. In the Highscores-Activity I want to read this file and put the content of the File in a List to print it on the Screen with a ListView. This is the code for writing the File:

ArrayList<Integer> highscoreListe = new ArrayList<Integer>();
punkte = getIntent().getExtras().getInt("Punkte");
public void writeToExternalStoragePublic() throws IOException
 {
     String filename = "highscores.txt"; 

     FileOutputStream fos= null;
     OutputStreamWriter osw = null;
     fos= openFileOutput(filename,Context.MODE_PRIVATE);
     osw = new OutputStreamWriter(fos);
     for(Integer score : highscoreListe)
     {
         osw.write(score);
         osw.append(System.getProperty("line.separator"));
     }
     osw.close();
     fos.close();
 }

I read a lot of Questions on stackoverflow about this but I still dont know how to read this file to put for every line in the file a new string or int into a list. I hope you understand what I want to do.

Marcel
  • 3
  • 3

1 Answers1

0

plese refe this link to wtire a file

http://developer.android.com/guide/topics/data/data-storage.html#pref

Writing to text file in "APPEND mode" in emulator-mode,

does the FileWriter deletes an existing file and creates a new file with the same name if the filename already exists

yes but consider

public FileWriter(File file,
                  boolean append)
append - if true, then bytes will be written to the end of the file rather than the beginning 

it you want to make your file private write in internal storage.

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • Ok well I think I didnt get the whole idea behind this. I mean the SharedMedia doesnt seems to be what i need, because you cant put a list of integers into it (just primitive types and a set of strings). The FileOutputStream just accepts bytes right? So I have a File which is filled with bytes. How do I transform these bytes back into strings when I want to read the File in another Activity and save the content into a list of integers? – Marcel Jun 14 '12 at 08:33
  • not exactly sure for what you looking ? as you can read data from while and can covert that to string and can parse that to int http://stackoverflow.com/questions/2902689/how-can-i-read-a-text-file-from-the-sd-card-in-android – Dheeresh Singh Jun 15 '12 at 20:14
  • https://www.google.co.in/search?rlz=1C1AVSX_enIN409IN409&sugexp=chrome,mod=17&sourceid=chrome&ie=UTF-8&q=android+reaing+data+from+file+ – Dheeresh Singh Jun 15 '12 at 20:17
  • Thanks dude, that really helped me to improve my understanding on this topic, but I still have a problem with the reading. I put a Update on my question, maybe you know what to do. – Marcel Jun 16 '12 at 09:23