Giới thiệu
Ví dụ này minh họa cách đọc file văn bản (text file) từ thư mục assets của ứng dụng Android sử dụng InputStream trong Kotlin.
Các bước thực hiện
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin.
Bước 2: Giao diện (res/layout/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"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="24dp"
android:textAlignment="center"
android:textColor="@android:color/holo_purple"
android:textStyle="bold" />
<Button
android:id="@+id/btnReadText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="30dp"
android:text="Read text from assets"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Tạo file assets
- Chuột phải module
app→ New → Folder → Assets Folder. - Chuột phải thư mục
assets→ New → File đặt tênmyText.txt. - Nhập nội dung bất kỳ vào file (ví dụ: "Hello from assets folder!").
Bước 4: MainActivity.kt
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
import java.io.InputStream
class MainActivity : AppCompatActivity() {
private lateinit var button: Button
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
textView = findViewById(R.id.textView)
button = findViewById(R.id.btnReadText)
button.setOnClickListener {
val string = readTextFromAssets("myText.txt")
textView.text = string
}
}
private fun readTextFromAssets(fileName: String): String {
return try {
val inputStream: InputStream = assets.open(fileName)
val size = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
inputStream.close()
String(buffer, charsets = Charsets.UTF_8)
} catch (e: IOException) {
e.printStackTrace()
"Error reading file: ${e.message}"
}
}
}
Bước 5: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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ị, nhấn Run. Nhấn nút "Read text from assets" → nội dung file myText.txt hiển thị trên TextView.
Mẹo: Dùng assets.open("subfolder/file.txt") để đọc file trong thư mục con. Với file lớn, nên đọc theo dòng (BufferedReader) hoặc dùng Coroutine IO để không block UI thread.