Phenomenal One
Phenomenal One

Reputation: 2587

Create a TXT File in SD Card and write into it in android not working

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

Answers (1)

Rathod Vijay
Rathod Vijay

Reputation: 417

You can change file object

File file=new File(Environment.getExternalStorageDirectory().toString()+ Environment.DIRECTORY_DOCUMENTS+"/myfile.txt");

Upvotes: 1

Related Questions