
What is Internal Storage ?
Internal Storage is the storage of the private data on the device memory. Internal Storage by default save and load file to the internal storage are private application.it can not other application access in this files.
When user uninstall application to stored files are removed.Internal storage are save or read data from internally memory. FileInputStream and FileOutputStream classes are used to read and write data from the file.
How to Writing File In Internal Storage ?
Internal Storage write data from the file to called openFileOutput() Method.and this method gives two parameter first parameter is file name and second parameter is mode.Mode is private,public etc.In this method return is FileOutputStream object.
Syntax of Writing File
1 |
FileOutputStream fos= openFileOutput("YourFileName",MODE_WORLD_READABLE); |
How to Read File Using Internal Storage ?
Internal Storage is read data from file to use this method of openFileInput() .In this method gives one parameter of file name and this method is return object of FileInputStream.
Syntax of Reading File
1 |
FileInputStream fis = openFileInput(filename); |
How to implement Internal Storage Example
Create activity_main.xml File Following this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_internal_storage" 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" android:orientation="vertical" tools:context="com.bhaumik.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:layout_marginTop="10dp" android:hint="Enter a Message" android:gravity="top" android:textSize="18dp" android:id="@+id/et_message"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Write Message" android:id="@+id/btn_write" android:textAllCaps="false" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Read Message" android:textAllCaps="false" android:id="@+id/btn_read"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="10dp" android:hint="Show Message" android:textSize="18dp" android:id="@+id/tv_show"/> </LinearLayout> |
Create MainActivity.java File Following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
package com.bhaumik; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { EditText editText; TextView textView; Button write,read; String file_name = "demo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.et_message); textView = (TextView) findViewById(R.id.tv_show); write = (Button) findViewById(R.id.btn_write); read = (Button) findViewById(R.id.btn_read); textView.setVisibility(View.GONE); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String msg = editText.getText().toString(); String file_name = "demo"; try { FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE); fileOutputStream.write(msg.getBytes()); fileOutputStream.close(); Toast.makeText(MainActivity.this,"Message Saved",Toast.LENGTH_SHORT).show(); editText.setText(""); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String messages; try { FileInputStream fileInputStream = openFileInput("demo"); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer stringBuffer = new StringBuffer(); while ((messages = bufferedReader.readLine()) != null) { stringBuffer.append(messages + "\n"); } textView.setText(stringBuffer.toString()); textView.setVisibility(View.VISIBLE); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } } |
Above the example of create input field of edittext and two button of read and write and then one textview to display read and write data.when you can type your name in the edittext and click to write button then in this data when you typed in edittext to stored internal storage and then you can click read button to display textview read data when you typed in the edittext.