Introduction
Store private data on the device memory. You can directly save your application data to the internal memory. By default data is saved in internal memory and other applications cannot access it. When you uninstall your application all files will be removed from the device.
The FileInoutStream and FileOutPutStream classes are used to read and write data into the files.
FileInputStream
The FileInputStream gets input bytes from a file. The FileInputStream class provides the facility to read streams of raw bytes such as image data. It extends the InputStream class. The following are methods provided by the fileInputStream class:
FileOutputStraem is an output stream for writing data to a file. It extends the OutputStream class. The following are methods provided by the outputStream class:
Step 2
Create an XML file with this:
-
public int read()
reads a single byte from this Stream and returns it as an integer in the range from 0 to 225. -
public void close()
closes the stream.
-
public void close()
closes the stream.
-
public void write()
writes a single byte to the stream.

- <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"
- tools:context=".MainActivity" >
- <LinearLayout
- android:id="@+id/linearlayout1"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:layout_width="fill_parent">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="File Name:" />
- <EditText
- android:id="@+id/edtText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:ems="10" >
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_below="@+id/linearlayout1"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:layout_width="fill_parent">
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Data:" />
- <EditText
- android:id="@+id/edtText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dp"
- android:ems="10" />
- </LinearLayout>
- <Button
- android:id="@+id/save_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="170dp"
- android:layout_marginTop="150dp"
- android:text="save" />
- <Button
- android:id="@+id/read_btn"
- android:layout_marginLeft="50dp"
- android:layout_marginTop="150dp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="read" />
- </RelativeLayout>
Step 3
Create a Java class file with this:
- package com.compsapp;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Context;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- EditText edt_FileName,edt_Data;
- Button save_btn,read_btn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edt_FileName=(EditText)findViewById(R.id.edtText1);
- edt_Data=(EditText)findViewById(R.id.edtText2);
- save_btn=(Button)findViewById(R.id.save_btn);
- read_btn=(Button)findViewById(R.id.read_btn);
- //Performing action on save button
- save_btn.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- String filename=edt_FileName.getText().toString();
- String data=edt_Data.getText().toString();
- FileOutputStream fos;
- try {
- File my_file = new File("/sdcard/"+filename);
- my_file.createNewFile();
- FileOutputStream fileOutputStream = new FileOutputStream(my_file);
- OutputStreamWriter OutWriter = new OutputStreamWriter(fileOutputStream);
- OutWriter.append(data);
- OutWriter.close();
- fileOutputStream.close();
- Toast.makeText(getApplicationContext(),filename + " saved",Toast.LENGTH_LONG).show();
- } catch (FileNotFoundException e) {e.printStackTrace();}
- catch (IOException e) {e.printStackTrace();}
- }
- });
- //Performing action on Read Button
- read_btn.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View arg0) {
- String filename=edt_FileName.getText().toString();
- StringBuffer Buffer = new StringBuffer();
- String DataRow = "";
- String aBuffer = "";
- try {
- File myfile = new File("/sdcard/"+filename);
- FileInputStream fIn = new FileInputStream(myfile);
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(fIn));
- while ((DataRow = bufferedReader.readLine()) != null) {
- aBuffer += DataRow + "\n";
- }
- bufferedReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();
- }
- });
- }
- @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;
- }
- }
Step 4
In the Android Manifest. xml file give the external storage permission as in the following:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.compsapp"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.compsapp.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>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- </manifest>
Step 5

Join the conversation! Your thoughts help the community grow.