-3

How can I create a file(using Java for Android) in my Application data directory and write an integer in it and then use that integer? I think I would save it in .txt or .xml file.

user3578286
  • 147
  • 4
  • 10
  • 1
    If you only need to save an integer, why would you write it to a file instead of using [`SharedPreferences`](http://developer.android.com/guide/topics/data/data-storage.html#pref)? – Bryan Herbst May 05 '14 at 13:56
  • what have you tried so far and what didn't work? If you haven't try anything yet, what's wrong with the ~zillion resources on the subject offered by google/android dev website? – njzk2 May 05 '14 at 13:57
  • +1 for Tanis. This seems like a very strange request. – SvenT23 May 05 '14 at 13:57
  • Here : http://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android – Shivam Verma May 05 '14 at 13:57
  • Tanis I think it is useful but i would create a file and than use SharedPreferences? – user3578286 May 05 '14 at 14:00
  • If you use `SharedPreferences`, you don't need to worry about file IO at all. The framework will take care of the details of how and where the data is saved. – Bryan Herbst May 05 '14 at 14:04
  • ooh) thanks i would try it – user3578286 May 05 '14 at 14:04

2 Answers2

0

If you would like to save the file to 'internal' memory (making the file only accessible to the app), then do the following:

 OutputStreamWriter outputWriter = new OutputStreamWriter(openFileOutput("somefile.xml", Context.MODE_PRIVATE));
 String fileContent = "<xml>xmlstuff</xml>";
 outputWriter.write(fileContent);
 outputWriter.close();
bkane521
  • 386
  • 2
  • 10
0

You can use the methods openFileOutput, and openFileInput from the context, you should have access to this methods through your Activity.

You can check it on bellow link http://developer.android.com/reference/android/content/Context.html#openFileInput(java.lang.String)