Save and Load Text Files in Android Internal Storage – Simple Notepad

This guide shows how to create a basic Android notepad app that writes user-entered text to a private file in internal storage using FileOutputStream and BufferedWriter, then reads it back on launch with FileInputStream and BufferedReader, complete with layout XML and full activity code.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
Save and Load Text Files in Android Internal Storage – Simple Notepad

In many Android apps a simple way to persist user notes is to store the text in a private .txt file inside the app’s internal storage. The article demonstrates this by building a minimal notepad that saves and loads its content.

The UI consists of a LinearLayout with a title TextView, an EditText for input, and a Button that triggers the save operation. The layout XML is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".StoreTextActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:text="记事本" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFC8C8C8" />

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="start"
        android:hint="请输入文本"
        android:inputType="textMultiLine" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="saveFile"
        android:text="保存" />

</LinearLayout>

The saveFile(String str) method opens a private file named badao.txt with openFileOutput, writes the supplied string via a BufferedWriter, shows a toast on success, and finally closes both the writer and the stream in a finally block.

public void saveFile(String str) {
    FileOutputStream fos = null;
    BufferedWriter writer = null;
    try {
        fos = openFileOutput("badao.txt", Context.MODE_PRIVATE);
        writer = new BufferedWriter(new OutputStreamWriter(fos));
        writer.write(str);
        Toast.makeText(this, "保存成功", Toast.LENGTH_LONG).show();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try { writer.close(); } catch (IOException e) { e.printStackTrace(); }
        }
        if (fos != null) {
            try { fos.close(); } catch (IOException e) { e.printStackTrace(); }
        }
    }
}

When the activity starts, onCreate calls load() to read any previously saved text. If the returned string is not empty, it populates the EditText and shows a "加载成功" toast.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store_text);
    editText = findViewById(R.id.edit_text);
    String str = load();
    if (!TextUtils.isEmpty(str)) {
        editText.setText(str);
        Toast.makeText(this, "加载成功", Toast.LENGTH_LONG).show();
    }
}

The load() method opens the same badao.txt with openFileInput, reads each line with a BufferedReader, appends it to a StringBuilder, and returns the combined text. All streams are safely closed in a finally block.

public String load() {
    FileInputStream fis = null;
    BufferedReader reader = null;
    StringBuilder content = new StringBuilder();
    try {
        fis = openFileInput("badao.txt");
        reader = new BufferedReader(new InputStreamReader(fis));
        String line;
        while ((line = reader.readLine()) != null) {
            content.append(line);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try { reader.close(); } catch (IOException e) { e.printStackTrace(); }
        }
        if (fis != null) {
            try { fis.close(); } catch (IOException e) { e.printStackTrace(); }
        }
    }
    return content.toString();
}

The article also provides the complete StoreTextActivity source, which combines the layout, the save and load methods, and the click handler that forwards the button press to saveFile(editText.getText().toString()). After running the app, entering text, and tapping "保存", the file is created under data/data/your.package.name/files. Relaunching the app shows the previously saved content, confirming that the internal‑storage I/O works as expected.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAndroidFile I/OXML LayoutNotepad AppInternal Storage
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.