Here we will use the file handling technique that is in java. The screenshots of the output f this program is given below.
Source Code
So here we go for the main activity.
package com.ex;
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.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class IsExample extends Activity implements OnClickListener
{
Button b1,b2;
EditText ed1;
String str="Hi, This is the example of Data Storage Using Internal Storage";
String filename="readme.txt";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
ed1=(EditText)findViewById(R.id.edit1);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
public void onClick(View arg0) {
if(arg0==b1){
try{
//This command will make file as private internal. It is only visible
//to your application
FileOutputStream fos=openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(str.getBytes());
fos.close();
Toast.makeText(this, "File is created", 20000).show();
}
catch(Exception e){
}
}
if(arg0==b2){
int i;
try{
StringBuffer fileContent=new StringBuffer();
FileInputStream fis=openFileInput(filename);
while((i=fis.read())!=-1){
fileContent.append((char)i);
}
String data=new String(fileContent);
ed1.setText(data);
fis.close();
}
catch(Exception e){
}
}
}
}
Source Code
So here we go for the main activity.
package com.ex;
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.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class IsExample extends Activity implements OnClickListener
{
Button b1,b2;
EditText ed1;
String str="Hi, This is the example of Data Storage Using Internal Storage";
String filename="readme.txt";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
ed1=(EditText)findViewById(R.id.edit1);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
public void onClick(View arg0) {
if(arg0==b1){
try{
//This command will make file as private internal. It is only visible
//to your application
FileOutputStream fos=openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(str.getBytes());
fos.close();
Toast.makeText(this, "File is created", 20000).show();
}
catch(Exception e){
}
}
if(arg0==b2){
int i;
try{
StringBuffer fileContent=new StringBuffer();
FileInputStream fis=openFileInput(filename);
while((i=fis.read())!=-1){
fileContent.append((char)i);
}
String data=new String(fileContent);
ed1.setText(data);
fis.close();
}
catch(Exception e){
}
}
}
}
For layout main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/button1"
android:layout_width="320px"
android:layout_height="wrap_content"
android:text="save"
>
</Button>
<Button
android:id="@+id/button2"
android:layout_width="320px"
android:layout_height="wrap_content"
android:text="open"
>
</Button>
<EditText
android:id="@+id/edit1"
android:layout_width="319px"
android:layout_height="52px"
android:textSize="18sp"
>
</EditText>
</LinearLayout>
No comments:
Post a Comment