Reading a file sample program in Android

By: Ashley Viewed: 180 times  Printer Friendly Format    


This sample android program shows you how read a file in Android. In this program an embedded file which is saved in your project under the res/drawable folder is opened and read in your android program. Once it is read, the contents of the file are shown in a Toast. A Toast is just a special alert that is shown on the screen and disappears after some time automatically,.

The FileDemo1.java file is as follows:

package com.javasamples;

//reading an embedded RAW data file

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.*;

public class FileDemo1 extends Activity {
	
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	try {
		PlayWithRawFiles();
	} catch (IOException e) {
		Toast.makeText(getApplicationContext(), 
					 "Problems: " + e.getMessage(), 1).show();
	}
}// onCreate

public void PlayWithRawFiles() throws IOException {      
	String str="";
	StringBuffer buf = new StringBuffer();			
	InputStream is = this.getResources().openRawResource(R.drawable.my_base_data);
	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	if (is!=null) {							
		while ((str = reader.readLine()) != null) {	
			buf.append(str + "\n" );
		}				
	}		
	is.close();	
	Toast.makeText(getBaseContext(), 
			buf.toString(), Toast.LENGTH_LONG).show();				
		

}// PlayWithSDFiles

} // FilesDemo4

The output of this program will be as shown in the android emulator below.

The main.xml file in your res/layout folder is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>





Most Viewed Articles (in Android )

Latest Articles (in Android)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Salaam can any one tell me how to connect android
View Tutorial          By: Ghani22 at 2011-10-04 18:51:48

2. Nice sample,thanks a lot.
View Tutorial          By: E.R.YURTTA┼× at 2012-09-12 20:24:55

3. nice tutorial man !
View Tutorial          By: yash at 2012-11-10 05:08:31

4. FINALLY! A place that gives complete examples (wi
View Tutorial          By: bminton at 2013-03-19 00:15:55

5. This is really a very good article <a herf=&quo
View Tutorial          By: Nilesh at 2014-09-09 05:18:45

6. I used a txt file and stored it under my drawable
View Tutorial          By: Sara at 2014-09-18 16:33:30

7. it is perfectly working...
View Tutorial          By: Venkatesh at 2014-09-21 06:54:39

8. Works thnx :D :D :D :D
View Tutorial          By: nik at 2014-11-09 20:44:29

9. Its working Great.
Here i want to ask one

View Tutorial          By: Manish at 2015-03-18 11:55:20

10. I want to read a txt file from internal storage an
View Tutorial          By: Anees at 2016-04-14 16:18:22