Showing posts with label Read and Write File. Show all posts
Showing posts with label Read and Write File. Show all posts

How to read and write text in file

Hear i explain you how to read and write text in file by pragmatically and how to save them.
First we Require to create one layout for input and output means write a text and read a text and another is code file for handle file.



Demo.java  //class file
package com.dhaval.demo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dhaval.demo.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class demo extends Activity {

    private EditText editText;
    private TextView textView;
    private final String filename = Environment.getExternalStorageDirectory() + "/txtfile.txt";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        editText = (EditText) findViewById(R.id.txtedit);
        textView = (TextView) findViewById(R.id.txtview);
    }

    public void writeFile(View view) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(filename, true);
            bw = new BufferedWriter(fw);
            bw.write(editText.getText().toString());


        } catch (IOException e) {
// TODO Auto-generated catch block            e.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
// TODO Auto-generated catch block                e.printStackTrace();
            }
        }
    }

    public void readFile(View view) {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(filename);
            br = new BufferedReader(fr);
            String line, text = "";
            while ((line = br.readLine()) != null) {
                if (text.equals(""))
                    text = line;
                else                    text += "\r\n" + line;
            }
            textView.setText(text);

        } catch (IOException e) {
// TODO Auto-generated catch block            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
// TODO Auto-generated catch block                e.printStackTrace();
            }
        }
    }

}

activity_demo.xml //layout file


<LinearLayout 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"
    android:orientation="vertical">
<EditText
  android:id="@+id/txtedit"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:hint="Enter Text Hear.."
    android:singleLine="false" />
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
      <Button
            android:id="@+id/btsave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="writeFile"
            android:text="Write File" />
        <Button
            android:id="@+id/btread"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="readFile"
            android:text="Read File" />
    </LinearLayout>
    <TextView
       android:id="@+id/txtview"
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="false" />
</LinearLayout>