code for saving in SD card,i have declared button save where registered onclicklistner as follows:
btn.setOnClickListener(new View.OnClickListener()
{
EditText filename =(EditText) findViewById(R.id.filename);
EditText filecontent =(EditText) findViewById(R.id.filecontent);
public void onClick(View view)
{
String str = filename.getText().toString();
String str2= filecontent.getText().toString();
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath()+"/MyFiles");
directory.mkdirs();
File file = new File(directory,"textfile.txt");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(str);
osw.write(" ");
osw.write(str2);
osw.close();
}
}
on load button i have registered onclicklistener while defined block as well
private static final int READ_BLOCK_SIZE = 100;
while load code is:
Button load = (Button) findViewById(R.id.load);
load.setOnClickListener(new View.OnClickListener() {
EditText filename =(EditText) findViewById(R.id.filename);
EditText filecontent =(EditText) findViewById(R.id.filecontent);
public void onClick(View v) {
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() +
"/MyFiles");
File file = new File(directory, "textfile.txt");
FileInputStream fIn = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
filename.setText(s);
filecontent.setText(s);
Toast.makeText(getBaseContext(),
"File loaded successfully!",
Toast.LENGTH_SHORT).show();
}
});
its showing all contents of files in each textbox!!i want filename to b extracted from content !!
Unless you need the file to be human readable, I’d suggest storing the two String values as serialized Java objects using DataInputStream/DataOutputStream:
Writing:
Reading:
Note, however, that it is recommended that all I/O to the SDCard be done in a worker thread. It’s not good practice to do it in an event handler like this. Much better to fire up an
AsyncTaskfor each.