Reading a file sample program in Android

By: Ashley Emailed: 1609 times Printed: 2077 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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>




Android Home | All Android Tutorials | Latest Android Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(6)


1. View Comment

nice tutorial man !

View Tutorial          By: yash at 2012-11-10 05:08:31
2. View Comment

FINALLY! A place that gives complete examples (with pictures!) rather than cryptic pieces of code. I am so thankful to you.

View Tutorial          By: bminton at 2013-03-19 00:15:55
3. View Comment

I used a txt file and stored it under my drawable folder but it says, expected resource of type raw?? It's about the openRawResource() method

View Tutorial          By: Sara at 2014-09-18 16:33:30
4. View Comment

it is perfectly working...

View Tutorial          By: Venkatesh at 2014-09-21 06:54:39
5. View Comment

Works thnx :D :D :D :D

View Tutorial          By: nik at 2014-11-09 20:44:29
6. View Comment

Its working Great.
Here i want to ask one thing suppose i want to take only first line of the file or only last line of the file. then what should i have to do here?
Thanks


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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ashley
Is autodiscover CNAME a must in Office 365
Directory Synchronization tool in Office 365
Configure Outlook Manually for Office 365
Review: Ruggear - The world's most Rugged Phone in Singapore
Solution to error: unable to open connection to server due to security error
Making your Alias email as 'Send As' in Office 365
Steps to Create Shared Mailbox in office 365
xxxx cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.
The user's Outlook Live mailbox is full.
Outlook Live couldn't connect to the IMAP server.
The user's mailbox has already been migrated.
An Outlook Live mailbox wasn't found for the user.
Outlook Live couldn't sign in to the user account on the IMAP messaging system.
Android Send SMS and Make a phone call in Android Application
Android Preferences - Using Preferences in Android Tutorial

More Tutorials in Android
Getting Started with Android
Solution to error: unable to open connection to server due to security error
Android Preferences - Using Preferences in Android Tutorial
Android Send SMS and Make a phone call in Android Application
Android Emulator
Intent in Android to call one activity from another activity.
Animation sample program in Android
Another Animation sample program in Android
ArrayAdapter sample program in Android
Spinner sample program in Android
GridView sample program in Android
Gallery sample program in Android
Date and Time sample program in Android
Progress bar and downloading a file sample program in Android
Reading a file sample program in Android

More Latest News
Most Viewed Articles (in Android )
Progress bar and downloading a file sample program in Android
ArrayAdapter sample program in Android
Reading a file sample program in Android
Intent in Android to call one activity from another activity.
Date and Time sample program in Android
Android Send SMS and Make a phone call in Android Application
Solution to error: unable to open connection to server due to security error
Spinner sample program in Android
GridView sample program in Android
Reading and Writing a file to SD card sample program in Android
AlertDialog sample program in Android
Animation sample program in Android
Gallery sample program in Android
Android Emulator
Android Preferences - Using Preferences in Android Tutorial
Most Emailed Articles (in Android)
Getting Started with Android
Android Emulator
Animation sample program in Android
ArrayAdapter sample program in Android
GridView sample program in Android
Reading a file sample program in Android
Reading and Writing a file to SD card sample program in Android
Android Send SMS and Make a phone call in Android Application
Solution to error: unable to open connection to server due to security error
AlertDialog sample program in Android
Spinner sample program in Android
Intent in Android to call one activity from another activity.
Android Preferences - Using Preferences in Android Tutorial
Error generating R.java from manifest
Another Animation sample program in Android