0

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...?

Coops5575
  • 431
  • 2
  • 5
  • 16

1 Answers1

1

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();
lenik
  • 23,228
  • 4
  • 34
  • 43
  • Ive tried the above code, and although it locates and opens correct file, it now over writes the file, does not add to it. – Coops5575 Nov 11 '12 at 10:21
  • Eclipse IDE will not let me add MODE_APPEND to code when trying it this way – Coops5575 Nov 11 '12 at 10:21
  • that's different question, which has already been solved here: http://stackoverflow.com/a/6209739/1329296 – lenik Nov 11 '12 at 10:36