top of page

How to Read Text File Using Android Application | Realcode4you

>>> Purpose

In this blog, we will learn how to read a text file in an Android program. We are going to make an application based on MVC (Model-View-Controller) architecture for practice.


>>> Steps

  • Outline

  • Create project

  • Prepare text file

  • Resources

  • UI Design: View (activity_main.xml)

  • Model: Container.java

  • Logic code: Controller (MainActivity.java)

  • Test


>>> Outline

Suppose we read a text file line by line. The following figure shows steps to read the text file.



  • STEP1: Create an “assets” folder and put the text file in the folder.

  • STEP2: Create an input stream object through which data are read from the text file using “Context.getAssets().open()”.

  • STEP3: With the input stream, we create a BufferedReader object.

  • STEP4: Read line by line using “readLine()” until the end of lines.


For practice, we will use MVC architecture as follows. “Container.java” is Model that loads data from text file (“number.dat”). “MainActivity.java” is Controller that processes data retrieved from Container. “activity_main.xml” is View that shows results that are received from controller.



>>> Create project

In this step, we create a project.


- Create a project, “ReadTextFile”.

o Select “File” – “New” – “New Project…”

o Choose “Empty Views Activity” template and click “Next”.

o On “Configure your project” window, fill out information. Click “Finish”.

▪ Project name: ReadTextFile

▪ Package name: edu.csustan.cs3810.readtextfile

▪ Save location: \codes\ReadTextFile

▪ Build configuration language: Groovy DSL (build.gradle)


>>> Prepare text file

In this step, we will prepare a text file that is loaded with the application.



- Create “assets” folder.

o “assets” folder is created.

- In “assets” folder, create “numbers.dat” with five numbers.

>>> Resources

In this step, we define constants in dimens.xml and themes.xml.



>>> UI Design: View (activity_main.xml)

In this step, we add UI components and arrange them on the screen.





>>> Model: Container.java

In this step, we create Model, “Container.java” to load data from text file.


[ variables]

- Define two variables shown in rectangle.


  • “context” is used to access “numbers.dat” in “assests” folder.

  • ArrayList nums will contain numbers after they are loaded from file.

  • “ArrayList” are automatically imported. Otherwise, click “ArrayList”. Then, click Alt+Enter key to import classes.


- Add code to import “Context” class as shown in rectangle. The error on “Context” is gone.


[ method: load()]



  • First, we access “assets” folder using “context.getAssets()” method.

  • Second, “open()” opens the file and returns input stream object.

  • Third, we create a buffered reader object with the input stream.

  • Last, we read a line using “reader.readLine()”, and separate a string “1,2,3,4,5” into an array of string-type numbers (e.g. “1”, “2”, “3”, “4”, and “5”) using “split(<delimeter>)”. Then, we put each number into array list (nums) after converting type from string to integer.


- Add code to import “AssetManager” class and “java.io” package as shown in rectangle. The errors in red color are gone.


[ constructor]

- Add a constructor (shown in rectangle) that calls load() method so that data are loaded when container object is created.


  • When container object is created, we create an ArrayList object where numbers are saved.

  • DO NOT TYPE “filePath:” that is shown automatically.


[ methods: getNums()]

Logic code: Controller (MainActivity.java)

In this step, we create methods to get numbers from a container, compute sum and average.


- Open “MainActivity.java”.


[ variable: container]

- Add codes as shown in rectangle. Do NOT remove the existing code.


[ methods: getNums(), getSum(), getAvg()]




[ method: compute]

- Add a method to be called by clicking “COMPUTE” button.


- Connect the button UI component to event handler.


>>> Test


3 Comments


Nice tutorial! Reading a text file from the assets folder is one of those fundamental Android skills that every beginner should learn. I also like that the example uses the MVC pattern—it makes the code much cleaner and easier to maintain as the project grows.

The step-by-step explanation is easy to follow, and it's great for anyone who wants to understand file handling instead of just copying code from the internet. Definitely a useful guide for students and new Android developers!

By the way, if you're taking a break from coding and want to check out something different, have a look at https://swiper.com.se/bonus/.

Like

This is a great hands-on project for anyone learning Android development. Reading a text file line by line is one of those fundamental skills that helps you understand file handling, data processing, and how MVC architecture keeps code organized and easier to maintain. Tutorials like this are especially useful because they combine theory with a practical example that developers can build on for more advanced applications. For those who also enjoy discovering new online platforms, it’s worth checking out Chicken Road PK.

Like

Reading a text file in an Android app is usually straightforward when the correct file location, permissions, and character encoding are handled properly. Using modern storage APIs and clear error handling can make the feature more reliable across different Android versions. More technology and online entertainment content is available at https://pistolo.edu.gr/.

Like

REALCODE4YOU

Realcode4you is the one of the best website where you can get all computer science and mathematics related help, we are offering python project help, java project help, Machine learning project help, and other programming language help i.e., C, C++, Data Structure, PHP, ReactJs, NodeJs, React Native and also providing all databases related help.

Hire Us to get Instant help from realcode4you expert with an affordable price.

USEFUL LINKS

Discount

ADDRESS

Noida, Sector 63, India 201301

Follows Us!

  • Facebook
  • Twitter
  • Instagram
  • LinkedIn

OUR CLIENTS BELONGS TO

  • india
  • australia
  • canada
  • hong-kong
  • ireland
  • jordan
  • malaysia
  • new-zealand
  • oman
  • qatar
  • saudi-arabia
  • singapore
  • south-africa
  • uae
  • uk
  • usa

© 2023 IT Services provided by Realcode4you.com

bottom of page