Open In App

How to Read a Text File in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

A text file is a type of file that can store a sequence of characters or text. These characters can be anything that is human-readable. Such kind of files does not have any formatting for the text like Bold, Italics, Underline, Font, Font Size, etc. A Text file in Android can be used for accessing or reading the information or text present in it. Meaning, information could be stored in a text file and could be accessed whenever required in the run-time. So, through this article, we will show you how you could read or fetch text from a text file in Android.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Create an asset folder

Please refer to Assets Folder in Android Studio to create an assets folder in Android Studio. We shall be creating a text file in the assets folder.

Step 3: Create a text file in the asset folder

We can create a text file by simply right-clicking on the assets folder, drag the mouse on new, and click on File. Now type in some desired name, add “.txt” extension, and press Enter. Another way of doing the same is creating a text file on Desktop and simply copying it into the assets folder. This is how our text file looks like:

MyText.txt:

GeeksforGeeks
A computer science portal for geeks

Step 4: Add a TextView in the layout file (activity_main.xml)

We will add a TextView in the layout to display the text from the text file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- A TextView to show the data from the text file-->
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"/>
  
</RelativeLayout>


Step 5: Write the below program in the main code (MainActivity.kt)

In the main code, we will be reading the text file and displaying the text from this file in the TextView. Please refer to the comments available at almost every line of code for better understanding.

Kotlin




import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import java.io.*
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declaring and initializing the TextView from the layout
        val myTextView = findViewById<TextView>(R.id.textView)
  
        // A string variable to store the text from the text file
        val myOutput: String
  
        // Declaring an input stream to read data
        val myInputStream: InputStream
  
        // Try to open the text file, reads 
        // the data and stores it in the string
        try {
            myInputStream = assets.open("MyText.txt")
            val size: Int = myInputStream.available()
            val buffer = ByteArray(size)
            myInputStream.read(buffer)
            myOutput = String(buffer)
  
            // Sets the TextView with the string
            myTextView.text = myOutput
  
        } catch (e: IOException) {
            // Exception
            e.printStackTrace()
        }
    }
}


Output:

As soon as the application launches, we can see that the text from the text file is displayed in the TextView.



Last Updated : 11 Aug, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads