Writing and reading from a file in Android.

openFileOutput() is the method defined in ContextWrapper class used for creating a FileOutputStream for writing to a file.
openFileInput() is the method defined in ContextWrapper class used for creating a FileInputStream for readinf reading from a file.
ContextWrapper is the parent class of ContextThemeWrapper which is the parent of the Activity class.

Write to a file.

private void writeFile(String stringToSave) throws IOException {
	FileOutputStream outStream = null;
	try {
		outStream = openFileOutput(fileName, Context.MODE_PRIVATE);
		outStream.write(stringToSave.getBytes());
		if (fileExists()) {
			displayText("File saved.");
		} else {
			displayText("File not saved.");
		}
	} catch (Exception e) {
		errorNotification(e.getMessage());
	} finally {
		outStream.close();
	}
}

Read from a file.

private String readFile() throws IOException {
	FileInputStream inStream = null;
	InputStreamReader myInputStreamReader = null;
	BufferedReader myBufferedReader = null;
	String retrievedString = "";
	try {
		inStream = openFileInput(fileName);
		myInputStreamReader = new InputStreamReader(inStream);
		myBufferedReader = new BufferedReader(myInputStreamReader);
		String readLineString = myBufferedReader.readLine();
		while (readLineString != null) {
			retrievedString = retrievedString + readLineString;
			readLineString = myBufferedReader.readLine();
		}
	} catch (IOException ioe) {
		errorNotification(ioe.getMessage());
	} finally {
		myBufferedReader.close();
		myInputStreamReader.close();
		inStream.close();
	}
	return retrievedString;
}

Delete a file.

private void deleteMyFile(String fileName) {
	File dir = getFilesDir();
	File file = new File(dir, fileName);
	boolean deleted = file.delete();
	if (deleted) {
		displayText("File deleted.");
	} else {
		displayText("File not deleted.");
	}
}

Check if a file exist.

private boolean fileExists() {
	File dir = getFilesDir();
	File file = new File(dir, fileName);
	return file.exists();
}

A complete demo Activity class for writing and reading from a file in Android

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FileStorageActivity extends Activity {
	private static final String fileName = "my_note.txt";
	private EditText etInputString;
	private Button btnWrite;
	private Button btnRead;
	private Button btnDelete;
	private TextView tvDisplay;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		getApplicationContext();
		etInputString = (EditText) findViewById(R.id.et_input_string);
		btnWrite = (Button) findViewById(R.id.btn_write);
		btnRead = (Button) findViewById(R.id.btn_read);
		btnDelete = (Button) findViewById(R.id.btn_delete);
		tvDisplay = (TextView) findViewById(R.id.tv_display);

		initButtonListeners();
	}

	private void initButtonListeners() {
		btnWrite.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (!fileExists()) {
					try {
						String inputString = etInputString.getText().toString();
						writeFile(inputString);
					} catch (IOException e) {
						errorNotification(e.getMessage());
					}
				} else {
					displayText(fileName + " already exists!");
				}
			}
		});

		btnRead.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (fileExists()) {
					try {
						String myString = readFile();
						displayText(myString);
					} catch (IOException e) {
						errorNotification(e.getMessage());
					}
				} else {
					displayText(fileName + " does not exist!");
				}
			}
		});

		btnDelete.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (fileExists()) {
					deleteMyFile(fileName);
				} else {
					displayText(fileName + " does not exist");
				}
			}
		});
	}

	private void writeFile(String stringToSave) throws IOException {
		FileOutputStream outStream = null;
		try {
			outStream = openFileOutput(fileName, Context.MODE_PRIVATE);
			outStream.write(stringToSave.getBytes());
			if (fileExists()) {
				displayText("File saved.");
			} else {
				displayText("File not saved.");
			}
		} catch (Exception e) {
			errorNotification(e.getMessage());
		} finally {
			outStream.close();
		}
	}

	private String readFile() throws IOException {
		FileInputStream inStream = null;
		InputStreamReader myInputStreamReader = null;
		BufferedReader myBufferedReader = null;
		String retrievedString = "";
		try {
			inStream = openFileInput(fileName);
			myInputStreamReader = new InputStreamReader(inStream);
			myBufferedReader = new BufferedReader(myInputStreamReader);
			String readLineString = myBufferedReader.readLine();
			while (readLineString != null) {
				retrievedString = retrievedString + readLineString;
				readLineString = myBufferedReader.readLine();
			}
		} catch (IOException ioe) {
			errorNotification(ioe.getMessage());
		} finally {
			myBufferedReader.close();
			myInputStreamReader.close();
			inStream.close();
		}
		return retrievedString;
	}

	private void deleteMyFile(String fileName) {
		File dir = getFilesDir();
		File file = new File(dir, fileName);
		boolean deleted = file.delete();
		if (deleted) {
			displayText("File deleted.");
		} else {
			displayText("File not deleted.");
		}
	}

	private boolean fileExists() {
		File dir = getFilesDir();
		File file = new File(dir, fileName);
		return file.exists();
	}

	private void displayText(String stringName) {
		tvDisplay = (TextView) findViewById(R.id.tv_display);
		tvDisplay.setText(stringName);
	}

	private void errorNotification(String message) {
		tvDisplay = (TextView) findViewById(R.id.tv_display);
		tvDisplay.setTextColor(getResources().getColor(R.color.red));
		tvDisplay.setText("ERROR: " + message);
	}
}

The layout file main.xml for the demo activity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et_input_string"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:hint="Wite something here..."/>

    <Button
        android:id="@+id/btn_write"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Write" />

    <Button
        android:id="@+id/btn_read"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Read" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Delete" />

    <TextView
        android:id="@+id/tv_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"/>

</LinearLayout>

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search