Write an Android Application for saving and loading a file

Q.6) Write an Android Application for saving and loading a file.

Ans.
Internal storage allows you to read and write to files that are associated with each application’s internal memory.These files cannot be accessed by other applications or users.When the application is uninstalled, these files are automatically removed as well.



Example:
In the given below example I have created two activity. MainActivity is used for saving the data and ActivityB is used in getting the data.
package com.example.filestorage;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText name,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.txtName);
password=(EditText)findViewById(R.id.txtPass);
}
FileOutputStream fos=null;
public void save(View v)
{
String text1=name.getText().toString();
String text2=password.getText().toString();
text1=text1+" ";
try
{
fos=openFileOutput("MYFILE.txt", MODE_PRIVATE);
fos.write(text1.getBytes());
fos.write(text2.getBytes());
}
catch(Exception e)
{
Toast.makeText(this, "ERROR ="+e.toString(), Toast.LENGTH_SHORT).show();
}
finally
{
try {
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

Toast.makeText(this, "Data saved successfully", Toast.LENGTH_SHORT).show();
}

public void next(View v)
{
Intent i=new Intent(this,ActivityB.class);
startActivity(i);
}
}

Code for ActivityB.Java class
package com.example.filestorage;
import java.io.FileInputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class ActivityB extends Activity {
EditText name,pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
name=(EditText)findViewById(R.id.txtName);
pass=(EditText)findViewById(R.id.txtPass);
}
FileInputStream fis;
public void load(View v)
{
//String text1=name.getText().toString();
//String text2=pass.getText().toString();
StringBuffer sb=new StringBuffer();
int data=-1;
try
{
fis=openFileInput("MYFILE.txt");
while((data=fis.read())!=-1)
{
sb.append((char)data);
}
String text1=sb.substring(0, sb.indexOf(" "));
String text2=sb.substring(sb.indexOf(" ")+1);
name.setText(text1);
pass.setText(text2);
}
catch(Exception e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
finally
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}


Post your comment

    Discussion

  • Please help us -Shivam Sarda (04/09/15)
  • Please help us by posting solved papers for DAA and OT