Monday, February 12, 2018

Simple Build How To Read And Write File In Android

In This Example We See How To Read And Write File In Android With The Use
of SharedPrefrence.

In This I Tack Following Controls.

1. Two Button
2.One Edittext
3. One Textview

Following Are The Code For Read And Write File In Android....


[1] Mainactivity.java

package com.demo.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

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

public class FilehandlingdemoActivity extends Activity
{
String FileName = "datafile";
Button save,read;
EditText e1;
TextView t1;
String name;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        save =(Button)findViewById(R.id.sbtn);
        read=(Button)findViewById(R.id.rbtn);
        e1=(EditText)findViewById(R.id.editText1);
        t1=(TextView)findViewById(R.id.textView1);
        save.setOnClickListener(new View.OnClickListener()
 {

public void onClick(View v)
 {
// TODO Auto-generated method stub

saveFile();

}


}
);
        read.setOnClickListener(new View.OnClickListener()
 {

public void onClick(View v)
 {
// TODO Auto-generated method stub
readFile();
}


}
);
     
    }
    private void saveFile()
    {
// TODO Auto-generated method stub
    String strname= e1.getText().toString();
    SharedPreferences sp = getSharedPreferences(FileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor e= sp.edit();
    e.putString("name", strname);
    e.commit();
    Toast.makeText(this, "Data Save Successfully", Toast.LENGTH_SHORT).show();
}
    private void readFile()
 {
// TODO Auto-generated method stub
    SharedPreferences sp = getSharedPreferences(FileName, Context.MODE_PRIVATE);
    String defaultvalue ="defaultname";
    String name= sp.getString("name", defaultvalue);
    t1.setText(name);
    Toast.makeText(this,"Data:"+name, Toast.LENGTH_SHORT).show();


}
}

How To Read And Write File In Android


[2] Main.xml File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="match_parent">
    <requestFocus></requestFocus>
</EditText>
<LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical">
    <Button android:id="@+id/rbtn" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="read"></Button>
    <Button android:text="save" android:id="@+id/sbtn" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>


</LinearLayout>


How To Read And Write File In Android


Following Are The Output Of Above Program


Output:-

How To Read And Write File In Android

How To Read And Write File In Android



No comments:

Post a Comment

Generate Even Numbers in a Range In PHP.

$start = 1; $end = 20; for ($i = $start; $i <= $end; $i++) {     if ($i % 2 == 0) {         echo $i . " is even.<br>";   ...