Open In App

How to Read a File in Android?

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:



Explanation

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).

Article Tags :