Android Write and Read File Tutorial

Android are able to let the android application to writing and reading text from a text file.  There have two storage option that allow the application to use which are internal and external storage. The internal storage which is embeded in the mobile device while external storage can be remove it or change it such as SD card. There have three ways to write and read the file which are InputStream/OutputStream(byte stream), InputStreamReader/OutputStreamWritter(charater stream) and BufferReader/BufferedWriter(per line). The InputStream/OutputStream is the oldest method to read and write per bytes which not efficient and slow. I use BufferedWriter to writing the text file while BufferedReader to reading the text file. In this tutorial, I will give you some steps to make android write and read file in application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “FileExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Add Permissions

Go to AndroidManifest.xml add two permissions which are writing external storage and reading external storage. You can copy the source code below.

Edit activity_main.xml layout

In your activity_main.xml add two button for read and write. After that add one editText and textView, you can following the sorce code below.

Edit MainActivity.java class

Copy and paste the following source code below, the two buttons perform the writing and reading the text file functionality.

Run your project

Now, you can run the project on your android device and also you can check the text file in your directory after you writing the text file in the application.

(Android Write and Read File)

Source Code

(Visited 220 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *