Open top menu


HOW TO READ AND WRITE FILE  INTERNAL STORAGE ?


activity_main.xml.
............................................................................................................
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"    
android:layout_width="match_parent"    
ndroid:layout_height="match_parent"    
android:orientation="vertical"    
android:layout_gravity="center"    
android:layout_margin="6dp"    
tools:context=".MainActivity">

    
<TextView        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:textColor="#066001"        
android:gravity="center"        
android:textStyle="bold"        
android:textSize="20dp"        
android:layout_margin="10dp"        
android:text="Android Beginner point"/>

    
<TextView        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"        
android:gravity="center"        
android:layout_marginTop="30dp"        
android:textAlignment="center"        
android:text="Android Read/Write File" />

    
<EditText        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"        
android:id="@+id/fname"        
android:hint="Text File Name" />

    
<EditText        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"        
android:id="@+id/ftext"        
android:layout_marginTop="10dp"        
android:hint="Write In Text File " />
    
<Button        
android:layout_width="100dp"        
android:layout_height="wrap_content"        
android:id="@+id/btnwrite"        
android:layout_gravity="center_horizontal"        
android:text="Write File" />

    
<TextView        
android:layout_width="fill_parent"        
android:layout_height="1dp"        
android:layout_marginTop="40dp"         
android:background="#066001" />
    
<Button        
android:layout_width="100dp"        
android:layout_height="wrap_content"        
android:id="@+id/btnread"        
android:layout_gravity="center_horizontal"        
android:text="Read File" />

    
<TextView        
android:layout_width="fill_parent"        
android:layout_height="wrap_content"        
android:textSize="36dp"        
android:id="@+id/filecon" />

</LinearLayout>
............................................................................................................


MainActivity
............................................................................................................
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class MainActivity extends Activity {
    EditText editTextFileName,editTextData;
    Button saveButton,readButton;
    TextView readtext;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextFileName=(EditText)findViewById(R.id.fname);
        editTextData=(EditText)findViewById(R.id.ftext);
        saveButton=(Button)findViewById(R.id.btnwrite);
        readButton=(Button)findViewById(R.id.btnread);
        readtext=(TextView)findViewById(R.id.filecon) ;
        //Performing Action on Read Button        saveButton.setOnClickListener(new OnClickListener(){

            @Override            public void onClick(View arg0) {
                String filename=editTextFileName.getText().toString();
                String data=editTextData.getText().toString();

                FileOutputStream fos;
                try {
                    fos = openFileOutput(filename, Context.MODE_PRIVATE);
                    fos.write(data.getBytes());
                    fos.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        readButton.setOnClickListener(new OnClickListener(){

            @Override            public void onClick(View arg0) {
                String filename=editTextFileName.getText().toString();
                StringBuffer stringBuffer = new StringBuffer();
                try {
                    //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader                    BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                            openFileInput(filename)));
                    String inputString;
                    //Reading data line by line and storing it into the stringbuffer                    while ((inputString = inputReader.readLine()) != null) {
                        stringBuffer.append(inputString + "\n");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
              readtext.setText(stringBuffer.toString());
            }

        });
    }


}
............................................................................................................


Tagged

0 comments