Wednesday, 28 October 2015

Internal Storage Example in Android

We are able to save or read data from the device internal memory. FileInputStream and FileOutputStream classes are used to read and write data into the file.
Here, we are going to read and write data to the internal storage of the device.

 activity_main.xml
-----------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main"
   
tools:context=".MainActivity"
   
android:orientation="vertical">

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText"
       
android:hint="enter file name"
       
android:layout_marginTop="5dp" />

    <
EditText
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:id="@+id/editText2"
       
android:hint="enter text"
       
android:layout_marginTop="5dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Create File and Save Text"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="10dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="View Text From Saved File"
       
android:id="@+id/button2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="10dp" />

    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:id="@+id/textView"
       
android:layout_marginTop="10dp"
       
android:layout_gravity="center" />
</
LinearLayout>

MainActivity.Java
----------------------------------
package com.android.anil.internalstorage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
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 {
    Button
savebtn,showbtn;
    EditText
e1,e2;
    TextView
t1;
    String
rec;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       
savebtn=(Button)findViewById(R.id.button);
       
showbtn=(Button)findViewById(R.id.button2);
       
e1=(EditText)findViewById(R.id.editText);
       
e2=(EditText)findViewById(R.id.editText2);
       
t1=(TextView)findViewById(R.id.textView);
       
savebtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String filename=
e1.getText().toString();
                String text=
e2.getText().toString();
               
try {
                    FileOutputStream fileOutputStream=openFileOutput(filename+
".txt",MODE_PRIVATE);
                   
try {
                        fileOutputStream.write(text.getBytes());
                        fileOutputStream.close();
                        Toast.makeText(MainActivity.
this, "File created and Text saved", Toast.LENGTH_SHORT).show();
                    }
catch (IOException e) {
                        e.printStackTrace();
                    }
                }
catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
       
showbtn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {

                
rec=e2.getText().toString();

               
try {
                    FileInputStream fiso=openFileInput(
rec+".txt");

                    StringBuffer stringbuffer=
new StringBuffer();
                   
int id1;
                   
while ((id1=fiso.read())!=-1){

                        stringbuffer.append((
char) id1);

                    }
                    fiso.close();
                   
t1.setText(stringbuffer.toString());
                    Toast.makeText(MainActivity.
this, "Data is : "+stringbuffer.toString(), Toast.LENGTH_SHORT).show();



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


            }
        });
    }
}

Output is:
----------------------------------------




No comments:

Post a Comment