How do i read text file in Android using kotlin?
Published April 24, 2021In this Android studio example i will show you how to read a simple text file in Android using kotlin code. In this code we will read a simple text file from raw folder inside resource directory. To read a text file from raw folder we will use resources.openRawResource() method.
Let's get started
Step 1: Create android application in Android studio
Step 2: Create a rw folder inside resource directory and add your text file
Step 3: Update xml file with below code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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:textColor="@color/teal_700"
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>
|
Step 4: Now its time to write functionality to read a text file from raw folder inside resource directory.
var string: String? = ""
val stringBuilder = StringBuilder()
val `is`: InputStream = this.resources.openRawResource(R.raw.textfile)
val reader = BufferedReader(InputStreamReader(`is`))
while (true) {
try {
if (reader.readLine().also { string = it } == null) break
} catch (e: IOException) {
e.printStackTrace()
}
stringBuilder.append(string).append("\n")
textView.text = stringBuilder
}
`is`.close()
|
Complete activity file like below
package com.rrtutors.kotlincource
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
class ReadTextFileActivity : AppCompatActivity() {
lateinit var textView:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_read_text_file)
textView=findViewById(R.id.textView)
}
fun readTextFile(view: View) {
var string: String? = ""
val stringBuilder = StringBuilder()
val `is`: InputStream = this.resources.openRawResource(R.raw.textfile)
val reader = BufferedReader(InputStreamReader(`is`))
while (true) {
try {
if (reader.readLine().also { string = it } == null) break
} catch (e: IOException) {
e.printStackTrace()
}
stringBuilder.append(string).append("\n")
textView.text = stringBuilder
}
`is`.close()
Toast.makeText(baseContext, stringBuilder.toString(),
Toast.LENGTH_LONG).show()
}
}
|
Step 5: Run application. On press on button the readTextFile() function will execute and read a text file from raw folder and set the text to textview widget.
Conclusion: In this Android example we learned how to read a text file in android using kotlin code.
2 ways to make underline text android textview example
Article Contributed By :
|
|
|
|
3150 Views |