Storage

Android has following options for you to save persistent data.We can choose depend on own specific needs, such as whether data should be private to over application or accessible to other application and also depend on how much space required by data.
The data storage options are the following.

  •     Shared Preferences
  •     Internal Storage
  •     External Storage
  •     SQLite Databases
  •     Network connection

Shared Preferences

Private data is saved as a key-value pair.

//Initialized SharedPreferences;
SharedPreferences sp;
  sp=getSharedPreferences("Test", MODE_PRIVATE);

//put data by Editor with the help of SharedPreferences
SharedPreferences.Editor ed=sp.edit();
ed.putString("name", name);
ed.putString("age", age);
ed.commit();

//get data by SharedPreferences
  String nm=sp.getString("name","");
  String  age=sp.getString("age", "");


Internal Storage

It's store private data on device memory and it's use only private data,Android has provide the facility to save data and read data from the device internal memory. We can use FileInputStream and FileOutputSream class to read and write data into file.


//write data in any file  

try {
//Initialized OutputStream 
OutputStream out=openFileOutput("Enter Your file name", MODE_PRIVATE);
  String data1="your test whenever you want";
  byte[]  databyte=data1.getBytes();
  out.write(databyte);
  out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//read data from any file  

    String name="Enter Your file name";
    String line= "";
try {
InputStream inp=openFileInput(name);
InputStreamReader is=new InputStreamReader(inp);
BufferedReader br=new BufferedReader(is);
while( br.readLine()!=null){
line=br.readLine();
data.setText(line);
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


External Storage

it's store data publicly in external memory like external memory card.

//write data in any file  

String file="Your File name"+".txt";
try {
  File f_path=Environment.getExternalStorageDirectory();
        File fl1=new File(f_path,file);
         FileWriter fw=new FileWriter(fl1);
     fw.write(data1);
     fw.close();
 Toast.makeText(sdcard.this, "file Write", 4).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


//read data from any file  

String name="Enter File Name"+".txt";
File fr=Environment.getExternalStorageDirectory();
File fi=new File(fr,name);
FileReader ff;
try {
ff = new FileReader(fi);
BufferedReader br=new BufferedReader(ff);
String st="";
while(true){
String dt=br.readLine();
if(dt==null)
break;
st+=dt+"\n";
}
data.setText(st);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


 SQLite Databases

It's store structured data and it use to perform database operations on android devices. SQLiteOpenHelper class is use for SQLite database.
for complete information with example click 

Network connection

it's store data in own server.
you can use following class for Network connection
java.net.*
android.net.*