file write and save the memory card in android

by mariganesh 2013-06-25 17:48:29

File write method in android

Here we have print the "Hello World" in "easy.html" using by android



public void insertdata(String result)
{

if(result.equalsIgnoreCase("Error"))
{
//String errorMessage = "Check Data Connection";
//showalert(errorMessage);
}
else
{
String FILENAME = "easy.html";
String result = "Hello world";
try
{


copyAssets();

FileOutputStream fos = openFileOutput(FILENAME, MODE_WORLD_READABLE);

fos.write(result.getBytes());

fos.close();

}catch(Exception ex){

ex.printStackTrace();
}




}

}







Copy file from asset to SDcard


private void copyAssets()
{
AssetManager assetManager = this.getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/sdcard/myfiles/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}

540
like
0
dislike
0
mail
flag

You must LOGIN to add comments