In this tutorial how to read data from the file in an Android Application is shown.
We can read and write data in an internal memory as well as external memory of our mobile.
When we read or write data in an external memory then we have to set permission in AndroidManifest.xml file, but when you read or write the data in internal memory then no permissions are required.
The permission to set for external storage to read the data in external file is as below:
<manifest>
<uses-permission android:name=”android:permission.READ_EXTERNAL_STORAGE”/>
</manifest>
Write the above code after the <uses-sdk> tag and before the <application> tag.
We can call openFileInput() method to get FileInputStream.
This is the code for file internal storage.
Code:
activity_main.xml
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >
<TextView
android:id=”@+id/fdt2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”69dp”
android:text=”File I/O Operations” />
<Button
android:id=”@+id/view1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”
android:onClick=”onFileView”
android:text=”View File” />
</RelativeLayout>
MainActivty.java
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onFileView(View v)
{
Intent in=new Intent(MainActivity.this,display.class);
startActivity(in);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
res/layout/display_layout.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >
<EditText
android:id=”@+id/fnm2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”78dp”
android:ems=”10″
android:hint=”Enter the File Name” >
<requestFocus />
</EditText>
<TextView
android:id=”@+id/fdt2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/fnm2″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”51dp” />
<Button
android:id=”@+id/back2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/display”
android:layout_alignBottom=”@+id/display”
android:layout_alignRight=”@+id/fnm2″
android:onClick=”back”
android:text=”Back” />
<Button
android:id=”@+id/display”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/fnm2″
android:layout_below=”@+id/fdt2″
android:layout_marginTop=”78dp”
android:onClick=”displayf”
android:text=”View File” />
</RelativeLayout>
src/com.package.demo/display.java
package com.example.demo;
import java.io.FileInputStream;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class display extends Activity{
FileInputStream fIn;
EditText fnm;
TextView dt2;
String fname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_layout);
fnm=(EditText)findViewById(R.id.fnm2);
dt2=(TextView)findViewById(R.id.fdt2);
}
public void displayf(View v) throws Exception
{
try{
fname=fnm.getText().toString();
fIn=openFileInput(fname);
int c;
String temp=””;
while((c=fIn.read())!=-1)
{
temp=temp+Character.toString((char)c);
}
dt2.setText(temp);
Toast.makeText(getBaseContext(), “File data read successfully”, Toast.LENGTH_SHORT).show();
fIn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
fIn.close();
}
}
public void back(View v)
{
Intent in=new Intent(this,MainActivity.class);
startActivity(in);
}
}
AndroidManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.display”></activity>
</application>
</manifest>