Reputation: 441
Please help... I have created a text file in the first Activity of my App, which is all working fine. In My next Activity, i want to append to the text file.
But when i try to append the catch throws up the following error..
mnt/scard/PatRecords/testfile.txt contains a file seperator
And nothing is added to the file.
my code for appending is..
try {
File directory = new File
(Environment.getExternalStorageDirectory().getPath()+"/PatRecords");
FileOutputStream fOut = openFileOutput(directory.getPath()+"/"+FileName$, MODE_APPEND);
OutputStreamWriter OutWriter = new OutputStreamWriter(fOut);
OutWriter.write(TestNo$+"\n");
OutWriter.write(Date$+"\n");
OutWriter.close();
fOut.close();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
Error=1;
}//End of try/catch
I have tried removing the seperators etc but still doesn't work, and as far as i can see the path shown in the catch error is correct...?
Upvotes: 0
Views: 489
Reputation: 23498
openFileOutput()
is for opening files in your program data folder, which is located on the internal memory, you may only supply the file name, not the path, hence the complain mnt/scard/PatRecords/testfile.txt contains a file separator
.
if you want to open files on the SD card, you have to use FileOutputStream()
or something like that:
File of = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.flush();
fos.close();
Upvotes: 1