Reputation: 2587
String fileName = "sample";
try {
final File f= new File(Environment.getExternalStorageDirectory(), fileName);
FileOutputStream fo = new FileOutputStream(f);
fo.write(code.getBytes());
fo.close();
Toast.makeText(getBaseContext(),"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
I need to copy a String named code
to my file named sample
.
If I do this, I don't get any errors. However, if use my phone and debug into my SD card, no File is created. Can anyone please tell me what I have done wrong??
Upvotes: 0
Views: 735
Reputation: 417
You can change file object
File file=new File(Environment.getExternalStorageDirectory().toString()+ Environment.DIRECTORY_DOCUMENTS+"/myfile.txt");
Upvotes: 1