What is Internal-File-Storage

There are many ways by which you can store the app data its depend on you and your need .The internal file storage allow you to store the file in internal device file system.

In my prgram user enter the username and password and click on save button it will save the data in abc.txt file and when you click on the display button it will display the username and password on textview.


Internal-File-Storage:.

In this section i am going to show the internal file storage with practical example.

First open the android studio and create a project and give the name of your project like InternalStorageDemo.

The screenshots of the program is as follows:

internal-storage image internal-storage image

Drag and drop the three TextView , one EditText , two Button and one password from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="64dp"
        android:layout_marginTop="128dp"
        android:text="UserName" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="92dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="206dp"
        android:text="Password" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:ems="10"
        android:inputType="textPassword"
        android:text="" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/textView"
        android:layout_marginBottom="170dp"
        android:onClick="save"
        android:text="save" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/button"
        android:layout_marginEnd="85dp"
        android:onClick="displayinfo"
        android:text="display" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="139dp"
        android:text="" />


</RelativeLayout>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    EditText username,password;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
        textView=(TextView)findViewById(R.id.textView3);



    }
    public  void save(View v)
    {


            String user=username.getText().toString();
            String pwd=password.getText().toString();
            FileOutputStream fileOutputStream;
            user=user+" ";

            try{ fileOutputStream = openFileOutput("abc.txt", Context.MODE_PRIVATE);
                fileOutputStream.write(user.getBytes());
                fileOutputStream.write(pwd.getBytes());


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e){

            e.printStackTrace();
        }finally {

        }

        Toast.makeText(MainActivity.this,"Saved",Toast.LENGTH_LONG).show();

    }

    public  void displayinfo(View v)
    {
        FileInputStream fileInputStream;
        StringBuffer buffer=new StringBuffer();
        try {
            fileInputStream = openFileInput("abc.txt");
            int read=-1;

            while((read=fileInputStream.read())!=-1)
            {
                buffer.append((char)read);

            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e)
        {
            e.printStackTrace();
        }
       textView.setText(buffer.toString());

    }

}



Explanation:

EditText username,password:This statement declare the object username,password of EditText.

TextView textView:This statement declare the object textView of TextView.

username=(EditText)findViewById(R.id.editText):This statement initialized the username object.

password=(EditText)findViewById(R.id.editText2);:This statement initialized the password object.

textView=(TextView)findViewById(R.id.textView3):This statement initialized the textView object.

public void save(View v):Here i have created the method save for saving the data that is coming form username,password object.

String user=username.getText().toString():
Here we take the value from username object with the help of getText() and after conversion it assign the user variable.

String pwd=password.getText().toString();:Here we take the value from password object with the help of getText() and after conversion it assign the pwd variable.

FileOutputStream fileOutputStream:Here we declare the fileOutputStream object of FileOutputStream class.

user=user+" ":This statement give the space after user value and again assing into the user variable.

try:here we take the try and catch statement for FileNotFoundException and IOException.

fileOutputStream = openFileOutput("abc.txt", Context.MODE_PRIVATE):The openFileOutPut() take the two argument first is file name and second is mode and return the instance of FileOutputStream.

fileOutputStream.write(user.getBytes()):The write() of FileOutputStream is used to write the info into file.Here write take the argument as byte[] so we convert the string into byte with the help of getByte().

fileOutputStream.write(pwd.getBytes()):Here we write the password into file.

Toast.makeText(MainActivity.this,"Saved",Toast.LENGTH_LONG).show():Here we are using the Toast for displaying the message after saving the data into file.

the save() we need to set the android:onClick property of save button.

public void displayinfo(View v):This method is used to get the info from file.

FileInputStream fileInputStream.Here we declare the fileInputStream object of the FileInputStream class to read the data from file.

StringBuffer buffer=new StringBuffer():Here we create the buffer object of the StringBuffer class to append the data that is read from the file.

fileInputStream = openFileInput("abc.txt"):The openFileInput() take the one argument as file name and return the instance of FileInputStream class further we use to loop for reading the data from file using read() of FileInputStream.

int read=-1:This statement declare the variable read and initialized the -1 value into it.

while((read=fileInputStream.read())!=-1):Here we are going to use the loop for reading the data from file and giving the condition of the loop when file return the -1 then loop will be terminated.

buffer.append((char)read):All the data read into read variable into byte format then the buffer object is used to append the data coming from file after converting into char as argument.

textView.setText(buffer.toString()):This statement set the buffer value into textView for display.


Internal file storage

what is internal file storage for video click here

back image                     next image