1

Possible Duplicate:
Android write to sd card folder

Writing a File with this Code:

public void writeFile(String data){
try { // catches IOException below
final String TESTSTRING = new String(data+"-");


String path=Environment.getExternalStorageDirectory().getAbsolutePath()
    + "/test.txt";
FileOutputStream fOut = openFileOutput(path,
                                        MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut); 

// Write the string to the file
osw.write(TESTSTRING);
/* ensure that everything is
 * really written out and close */
osw.flush();
osw.close();

}catch (Exception e) {
    e.printStackTrace();
}
}

Error Generated by Above Code:

java.lang.IllegalArgumentException: File /sdcard/test.txt contains a path separator  

I couldn't find what's wrong with this code.

Community
  • 1
  • 1
Nirav
  • 5,700
  • 4
  • 34
  • 44

2 Answers2

1

openFileOutput is is used for creating private files for the application which will be saved in the application's private directory.That means you need to send a file name to the method not the whole path.

And also make sure that you have the WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml .

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
0

openFileOuput() doesn't accept paths, only a file name.

Reno
  • 33,594
  • 11
  • 89
  • 102