Data Storage in Android

The storage system in Android are mainly divided into 3 types.


Database is already explained in my older post and you can see here


Android can save files directly to the device internal storage. These files are private to the application and will be removed if you uninstall the application.

We can create a file using openFileOutput() with parameters as file name and the operating mode. Similarly, we can open the file using openFileInput() passing the parameter as the fileName. One thing, that we need to remeber is, give the file name with extension. In this example I am creating a text(.txt) file.

The complete code is,

public class ReadNWriteFile extends Activity {

final String TEST_STRING = new String("Hello Android");
final String FILE_NAME = "SAMPLEFILE.txt";

public void onCreate(Bundle savedInstanceState) {
TextView tv = new TextView(this);

private void fileCreate() {
try {
OutputStream os = openFileOutput(FILE_NAME, MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(os);
} catch (Exception e) {
Log.i("ReadNWrite, fileCreate()", "Exception e = " + e);

private String readFile() {
try {
FileInputStream fin = openFileInput(FILE_NAME);
InputStreamReader isReader = new InputStreamReader(fin);
char[] buffer = new char[TEST_STRING.length()];
// Fill the buffer with data from file;
return new String(buffer);
} catch (Exception e) {
Log.i("ReadNWrite, readFile()", "Exception e = " + e);
return null;


SharedPreference is another persistent storage mechanism in Android. In this, we can store the data in key-value pairs. One drawback is it allows only primitive data types and String. We can get an object of shared preference using getSharedPreferences(). One thing, we need to remember is after writing anything to sharedPreference, we need to do commit(). Then only new values will be reflected. We can write values into sharedPreference using put() methods like putInt(), putString() etc and can retrieve the values from sharedPreference using get() methods as getInt(), getString() etc.

A sample program is,

public class DataStorage extends Activity {

public void onCreate(Bundle savedInstanceState) {
SharedPreferences pref = getSharedPreferences("Preference",
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("keyBoolean", true);
editor.putFloat("keyFloat", 1.0f);
editor.putInt("keyInt", 1);
editor.putLong("keyLong", 1000000L);
editor.putString("keyString", "Hello Android");

boolean dataFromPrefBool = pref.getBoolean("keyBoolean", false);
float dataFromPrefflaot = pref.getFloat("keyFloat", 0.0f);
int dataFromPrefInt = pref.getInt("keyInt", 0);
long dataFromPrefLong = pref.getLong("keyLong", 0);
String dataFromPrefString = pref.getString("keyString", null);

In the put(), we are passing key and value and in get(), we are passing key and the default value if the key is not present in the sharedPreference.


  1. this post was very helpful teaching me about the SharedPreferences really quickly. thanks! :)


Post a Comment

Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android