Skip to content
Related Articles

Related Articles

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

How to Read a File in Android?

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 19 Sep, 2021

In android development, data are presents in a form of files, shared preferences, and databases. We have data in different format according to task our app need to do. In the case of a video player or music player app, we have data store in files and how to read those files in Android we learn in this article. Here, First  I will store a file (JSON file in my example) in the Assets folder.

quotes.json is a JSON file, you can choose files of another format as well

In any file on computer data are store in binary form i.e. 0’s and 1’s. As we can’t read these files on our own, to read in the human-readable form we need to parse the file.

So to read the file and parse it for us we will follow these steps:

  • Step 1: We will use InputStream to open the file and we will stream the data into it.
  • Step 2: Create a variable to store the size of the file.
  • Step 3: Create a buffer of the size of the file.
  • Step 4: We will read the inputStream file into the buffer.
  • Step 5: Close the inputStream file.
  • Step 6: Convert the buffer file to the format in which you need your data.


  • InputStream is a class in JAVA that is used to read in an orderly sequence of bytes. Using it we can read files on android.
  • So why we are using buffer ?, Well we can do this job without it as well but buffer helps to read the file very efficiently.
  • InputStream reads the single bytes at a time, so it is better to read the array of bytes at a time for better performance.
  • So buffer helps us to convert the bytes into an array of bytes which can be read easily once at a time.

The last 3 lines are to change the Data in the buffer to JSON data because initially I have store the file in JSON format and then  I have converted the JSON data to JAVA object using GSON( You can ignore the last 3 lines if your file format is different).

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!