
Mainactivity.Java
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 |
package com.example.internalmemory; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText textmsg; static final int READ_BLOCK_SIZE = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textmsg=(EditText)findViewById(R.id.editText1); } // write text to file public void WriteBtn(View v) { // add-write text into file try { FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_PRIVATE); OutputStreamWriter outputWriter=new OutputStreamWriter(fileout); outputWriter.write(textmsg.getText().toString()); outputWriter.close(); //display file saved message Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } // Read text from file public void ReadBtn(View v) { //reading text from file try { FileInputStream fileIn=openFileInput("mytextfile.txt"); InputStreamReader InputRead= new InputStreamReader(fileIn); char[] inputBuffer= new char[READ_BLOCK_SIZE]; String s=""; int charRead; while ((charRead=InputRead.read(inputBuffer))>0) { // char to string conversion String readstring=String.copyValueOf(inputBuffer,0,charRead); s +=readstring; } InputRead.close(); Toast.makeText(getBaseContext(), s,Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } } |
Activity_Main.XML
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 |
<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="{relativePackage}.${activityClass}" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:background="#008080" android:padding="5dp" android:text="Android Read and Write Text form File" android:textColor="#fff" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="22dp" android:ems="10" android:layout_margin="5dp"> <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/editText1" android:text="Write Text into File" android:onClick="WriteBtn" android:layout_margin="5dp"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:text="Read Text From file" android:onClick="ReadBtn" android:layout_margin="5dp" /> </RelativeLayout> |