ภาพรวม
ตัวอย่างนี้แสดงวิธีการอ่านไฟล์ข้อความจากโฟลเดอร์ res/raw/ โดยใช้ InputStream และ BufferedReader
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่และเพิ่มไฟล์ข้อความ
- เปิด Android Studio ไปที่ File → New Project
- คลิกขวาที่
res→ New → Android Resource Directory - เลือก Resource type:
rawตั้งชื่อโฟลเดอร์เป็นraw - วางไฟล์ข้อความ (เช่น
sample.txt) ลงในres/raw/
ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="8dp"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:onClick="ReadTextFile"
android:text="Read Text File" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Read text File in Android"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)
package com.example.readtextfile;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
public void ReadTextFile(View view) {
StringBuilder stringBuilder = new StringBuilder();
try {
// เปิดไฟล์จาก res/raw/sample.txt
InputStream is = getResources().openRawResource(R.raw.sample);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
textView.setText(stringBuilder.toString());
is.close();
Toast.makeText(this, stringBuilder.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error reading file: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
ขั้นตอนที่ 4: ตั้งค่า Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.readtextfile">
<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>
การทดสอบ
รันแอปพลิเคชันบนอุปกรณ์จริง กดปุ่ม "Read Text File" จะอ่านไฟล์ sample.txt แสดงผลบน TextView และ Toast

ข้อมูลเพิ่มเติม
- ไฟล์ใน
res/raw/จะถูกคอมไพล์เข้ากับ APK ไม่สามารถแก้ไขได้ขณะรัน - หากต้องการเขียน/แก้ไขไฟล์ ให้ใช้
getFilesDir()หรือgetExternalFilesDir()แทน - รองรับไฟล์ขนาดใหญ่ได้ แต่ควรใช้ Thread แยกเพื่อไม่บล็อก UI