how to get data from internal file storage in android

watch_later Monday, August 1, 2016
In one of my  post we learned about how to save data to file internally in android.today we are going to learn about get data from internal file storage by using below concepts

* FileInputStream
DataInputStream
* BufferedReader

if you want to  save data to file internally just refer here

Create a project

1) Layout file

create UI file in res/layout folder

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">

    <TextView
        android:id="@+id/datatext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="@color/material_blue_grey_900"
        android:layout_centerInParent="true"/>


</RelativeLayout>

2)MainActivity.java

Just add the below code in your java file

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView datatext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
datatext=(TextView)findViewById(R.id.datatext);
            String filename = "internalsave.txt";
       
           String filepath = "sample";
            File myInternalFile;
     
               ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
           
File directory = contextWrapper.getDir(filepath, Context.MODE_WORLD_READABLE);
               myInternalFile = new File(directory , filename);
           
             
               String saveddata="";
               try {
       FileInputStream fis = new FileInputStream(myInternalFile);
       DataInputStream in = new DataInputStream(fis);
       BufferedReader br =
        new BufferedReader(new InputStreamReader(in));
       String strLine;
       while ((strLine = br.readLine()) != null) {
        saveddata = saveddata + strLine;
       
       }
       in.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
     
             datatext.setText(saveddata);
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.forblog"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
   
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>
    </application>


</manifest>


Finally, Run your application

OUTPUT:

we will get the output as shown below







sentiment_satisfied Emoticon