Ví dụ này minh họa cách tạo file txt và đọc file txt từ bộ nhớ trong (internal storage) trên Android.
Bước 1: Tạo dự án mới
Tạo dự án mới trong Android Studio: File → New Project, điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
xmlns:tools = "https://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity"
android:orientation = "vertical">
<EditText
android:id = "@+id/enterText"
android:hint = "Nhập văn bản tại đây"
android:layout_width = "match_parent"
android:layout_height = "wrap_content" />
<Button
android:id = "@+id/save"
android:text = "Lưu"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
<TextView
android:id = "@+id/output"
android:layout_width = "wrap_content"
android:textSize = "25sp"
android:layout_height = "wrap_content" />
</LinearLayout>
Giao diện có EditText và Button. Khi nhấn nút Lưu, dữ liệu từ EditText sẽ được lưu vào bộ nhớ trong tại đường dẫn /data/data/<your.package.name>/files/text/sample.txt. Nội dung file sẽ được hiển thị lên TextView.
Bước 3: Code MainActivity.java
package com.example.andy.myapplication;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView output = findViewById(R.id.output);
final EditText enterText = findViewById(R.id.enterText);
save = findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!enterText.getText().toString().isEmpty()) {
File file = new File(MainActivity.this.getFilesDir(), "text");
if (!file.exists()) {
file.mkdir();
}
try {
File gpxfile = new File(file, "sample");
FileWriter writer = new FileWriter(gpxfile);
writer.append(enterText.getText().toString());
writer.flush();
writer.close();
output.setText(readFile());
Toast.makeText(MainActivity.this, "Đã lưu văn bản", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
private String readFile() {
File fileEvents = new File(MainActivity.this.getFilesDir() + "/text/sample");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(fileEvents));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return text.toString();
}
}
Bước 4: Cấu hình AndroidManifest.xml
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
package = "com.example.andy.myapplication">
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
<activity android:name = ".MainActivity">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị, chạy ứng dụng từ Android Studio. Màn hình hiển thị giao diện mặc định.

Nhập văn bản và nhấn Lưu như hình dưới:

Kiểm tra file tại /data/data/<your.package.name>/files/text/sample.txt:
