2

Okay, I have come across many tutorials on creating and writing/reading a file. What I want my Android app is to create a small text file where it will read and write some settings so my user information can be saved. The problem is every tutorial I have come across uses the SDcard (which allows the user and any other app to read) or use MODE_WORLD_READABLE which makes any app read off of it.

I was wondering how I can create and read/write my file but private or atleast keep it on the internal storage of the phone.

nandeesh
  • 24,740
  • 6
  • 69
  • 79
Moyle Jack
  • 51
  • 5
  • 1
    Why not just use SharedPreferences? [1]: http://stackoverflow.com/questions/5734721/android-shared-preferences – rundavidrun Oct 21 '12 at 05:40
  • @rundavidrun dont know how to use it much and from what im seeing at least, its not much of a difference unless im wrong. – Moyle Jack Oct 21 '12 at 05:43
  • @Moyle Jack: you are wrong. It manages all you wanna do by yourself, it is standard Android API, it is bug free, ... Better learn it than do something buggy, you'll save time. – Vincent Mimoun-Prat Oct 21 '12 at 08:23

2 Answers2

3

Here's an easy example. To read a preference do this:

SharedPreferences preferences = getSharedPreferences("YourAppName", MODE_PRIVATE);
String thisString = preferences.getString("KeyForThisString", "default");

You can getInt, getLong, etc.

To store the preferences, use these lines:

SharedPreferences preferences = getSharedPreferences("YourAppName",MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("KeyForThisString", thisString);
editor.commit();

As before, you can putInt, putLong, etc.

The available methods for this API are here: http://developer.android.com/reference/android/content/SharedPreferences.html

rundavidrun
  • 933
  • 10
  • 19
1

dont know how to use it much and from what im seeing at least, its not much of a difference unless im wrong.

I still recommend to use SharedPreferences technique. It's explicitly easy and save a lot of time.

You can use it to save any primitive data: booleans, floats, ints, longs, and strings. For example, you can do that each time when user invokes any changes to your Application because it's high performance technique. By this way even on crash all edited info will be stored.

Suppose you have an Activity.

Use this method where data is your String that you wanted to save to file.

private void saveInfo(String data){
   String key = "myKey"; 
   SharedPreferences.Editor editor = mPrefs.edit();
   editor.putString(key , data);
   editor.commit();
}

Now, when you launch your application again onCreate method invoked and you can load your information back:

  private void loadInfo(){
     String key = "myKey"; 
     SharedPreferences mPrefs = context.getSharedPreferences(LauncherUI.PREFS_NAME, 0);
     String yourData = mPrefs.getString(key, ""); // "" means if no data found, replace it with empty string
}

Here is a link to 5 types to store data: Storage Options

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225