Readin n Writin

Reading and writing your own, application-specific data files is nearly identical to what you might do in a desktop Java application. The key is to use openFileInput() and openFileOutput() on your Activity or other Context to get an InputStream and OutputStream, respectively. From that point forward, the process is not much different from using regular Java I/O logic:

• Wrap those streams as needed, such as using an InputStreamReader or OutputStreamWriter for text-based I/O.

• Use close() to release the stream when done.

If two applications both try reading a notes.txt file via openFileInput(), they will each access their own edition of the file. If you need to have one file accessible from many places, you probably want to create a content provider, as will be described in Chapter 28.

Note that openFileInput() and openFileOutput() do not accept file paths (e.g., path/to/ file.txt), just simple filenames.

The following code shows the layout for the world's most trivial text editor, pulled from the Files/ReadWrite sample application available on the Apress Web site:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="" android:layout width="fill parent" android:layout height="fill parent" android:orientation="vertical"> <Button android:id="@+id/close" android:layout width="wrap content" android:layout height="wrap content" android:text="Close" /> <EditText android:id="@+id/editor" android:layout width="fill parent" android:layout height="fill parent" android:singleLine="false" />


All we have here is a large text-editing widget with a Close button above it. The Java is only slightly more complicated:


import; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import; import; import; import; import; import;

public class ReadWriteFileDemo extends Activity { private final static String NOTES="notes.txt"; private EditText editor;

^Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); editor=(EditText)findViewById(;

Button btn=(Button)findViewById(;

btn.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { finish();

public void onResume() { super.onResume();

InputStream in=openFileInput(NOTES);

if (in!=null) { InputStreamReader tmp=new InputStreamReader(in); BufferedReader reader=new BufferedReader(tmp); String str;

StringBuffer buf=new StringBuffer();

while ((str = reader.readLine()) != null) { buf.append(str+"\n");


catch ( e) { // that's OK, we probably haven't created it yet

catch (Throwable t) { Toast

.makeText(this, "Exception: "+t.toString(), 2000) .show();

public void onPause() { super.onPause();

OutputStreamWriter out=

new OutputStreamWriter(openFileOutput(NOTES, 0));

out.write(editor.getText().toString()); out.close();

catch (Throwable t) { Toast

.makeText(this, "Exception: "+t.toString(), 2000) .show();

First we wire up the button to close out our activity when it's clicked, by using setOnClickListener() to invoke finish() on the activity.

Next we hook into onResume() so we get control when our editor is coming to life, from a fresh launch or after having been frozen. We use openFileInput() to read in notes.txt and pour the contents into the text editor. If the file is not found, we assume this is the first time the activity was run (or that the file was deleted by other means), and we just leave the editor empty.

Finally we hook into onPause() so we get control as our activity gets hidden by another activity or is closed, such as via our Close button. Here we use openFileOutput() to open notes.txt, into which we pour the contents of the text editor.

The net result is that we have a persistent notepad: whatever is typed in will remain until deleted, surviving our activity being closed, the phone being turned off, and similar situations.

0 0

Post a comment