Save data into file

To save data into file in an android application, you have to follow the steps given bellow.

1. Create an object of the FileOutputStream class using the openFileOutput method.
    Example :  
                   FileOutputStream fou = openFileOutput("text.txt", MODE_WORLD_READABLE);
  
   Here text.txt is the name of the file. You can use any of the file opening mode
   a.    MODE_PRIVATE  :  File is accessible by only the application that create it.
   b.    MODE_APPEND :  Appending an existing file.
   c.    MODE_WORLD_WRITABLE : Allow write permission to all the applications. 

2. Get an object of the OutPutStreamWriter class using the FileOutputStream object.
    Example:
                 OutputStreamWriter osw = new OutputStreamWriter(fou);

3. Write the data using the write() method.
   Example:
                 osw.write(Message);

4. For reading the data from the file, first you need to create an object of the FileInputStream class.
   Example:
                 FileInputStream fis = openFileInput("text.txt");

5. Get an object of the InputStreamReader class .
   Example:
                InputStreamReader isr = new InputStreamReader(fis);

6. Finally read the data from the file as separate block of data having fixed size.

             





activity_main.xml
1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
2: xmlns:tools="http://schemas.android.com/tools"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: android:paddingBottom="@dimen/activity_vertical_margin"
6: android:paddingLeft="@dimen/activity_horizontal_margin"
7: android:paddingRight="@dimen/activity_horizontal_margin"
8: android:paddingTop="@dimen/activity_vertical_margin"
9: tools:context=".MainActivity" >
10: <EditText
11: android:id="@+id/msg"
12: android:layout_width="wrap_content"
13: android:layout_height="wrap_content"
14: android:layout_alignParentTop="true"
15: android:layout_centerHorizontal="true"
16: android:layout_marginTop="20dp"
17: android:ems="10"
18: android:hint="Enter a message"
19: >
20: <requestFocus />
21: </EditText>
22: <Button
23: android:id="@+id/LOAD"
24: android:layout_width="wrap_content"
25: android:layout_height="wrap_content"
26: android:layout_alignLeft="@+id/SAVE"
27: android:layout_below="@+id/SAVE"
28: android:layout_marginTop="46dp"
29: android:text="LOAD DATA" />
30: <Button
31: android:id="@+id/SAVE"
32: android:layout_width="wrap_content"
33: android:layout_height="wrap_content"
34: android:layout_below="@+id/msg"
35: android:layout_centerHorizontal="true"
36: android:layout_marginTop="23dp"
37: android:text="SAVE DATA" />
38: </RelativeLayout>

MainActivity.java
1:  package com.filedemo;  
2: import java.io.FileInputStream;
3: import java.io.FileNotFoundException;
4: import java.io.FileOutputStream;
5: import java.io.IOException;
6: import java.io.InputStreamReader;
7: import java.io.OutputStreamWriter;
8: import android.app.Activity;
9: import android.os.Bundle;
10: import android.view.Menu;
11: import android.view.View;
12: import android.view.View.OnClickListener;
13: import android.widget.Button;
14: import android.widget.EditText;
15: import android.widget.Toast;
16: public class MainActivity extends Activity {
17: Button save,load;
18: EditText message;
19: String Message;
20: int data_block = 100;
21: @Override
22: protected void onCreate(Bundle savedInstanceState) {
23: super.onCreate(savedInstanceState);
24: setContentView(R.layout.activity_main);
25: save=(Button) findViewById(R.id.SAVE);
26: load = (Button) findViewById(R.id.LOAD);
27: message = (EditText) findViewById(R.id.msg);
28: save.setOnClickListener(new OnClickListener() {
29: @Override
30: public void onClick(View v) {
31: // TODO Auto-generated method stub
32: Message = message.getText().toString();
33: try {
34: FileOutputStream fou = openFileOutput("text.txt", MODE_WORLD_READABLE);
35: OutputStreamWriter osw = new OutputStreamWriter(fou);
36: try {
37: osw.write(Message);
38: osw.flush();
39: osw.close();
40: Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_LONG).show();
41: } catch (IOException e) {
42: // TODO Auto-generated catch block
43: e.printStackTrace();
44: }
45: } catch (FileNotFoundException e) {
46: // TODO Auto-generated catch block
47: e.printStackTrace();
48: }
49: }
50: });
51: load.setOnClickListener(new OnClickListener() {
52: @Override
53: public void onClick(View v) {
54: // TODO Auto-generated method stub
55: try {
56: FileInputStream fis = openFileInput("text.txt");
57: InputStreamReader isr = new InputStreamReader(fis);
58: char[] data = new char[data_block];
59: String final_data="";
60: int size;
61: try {
62: while((size = isr.read(data))>0)
63: {
64: String read_data = String.copyValueOf(data, 0, size);
65: final_data+= read_data;
66: data = new char[data_block];
67: }
68: Toast.makeText(getBaseContext(), "Message :"+final_data, Toast.LENGTH_LONG).show();
69: } catch (IOException e) {
70: // TODO Auto-generated catch block
71: e.printStackTrace();
72: }
73: } catch (FileNotFoundException e) {
74: // TODO Auto-generated catch block
75: e.printStackTrace();
76: }
77: }
78: });
79: }
80: @Override
81: public boolean onCreateOptionsMenu(Menu menu) {
82: // Inflate the menu; this adds items to the action bar if it is present.
83: getMenuInflater().inflate(R.menu.main, menu);
84: return true;
85: }
86: }