1

In the below code I am trying to append some text to a file using FileOutputStream and BufferedWriter as shown below.At ru time, despite the file has some data, when i use FileOutputStream and BufferedWriter i found the file is empty and even the data i want to append bw.write("new information"); is not existing the file is completely empty.

Kindly please let me know how to fix it.

Code:

File f = new File(SystemConfig.getSystConfigInstance("E"));
    System.out.println(f.getAbsolutePath() + " name: " + f.getName());

    OutputStream os = new FileOutputStream(f);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
    bw.write("new information");
rmaik
  • 1,482
  • 2
  • 12
  • 39
  • 1
    it would be good to call bw.flush() after write if you want to see immediately effect – Adem Dec 15 '14 at 08:49
  • @Adem would you please provide brie explanation about flush? – rmaik Dec 15 '14 at 08:50
  • streams mostly have some buffer. when you are writing some data on them, they first keep this data until their buffer gets full. after then, they performed actual writing. you can force this with calling "flush()" that writes current buffer data to destination – Adem Dec 15 '14 at 08:52

3 Answers3

2

Try this, FileOutputStream(File file, boolean append) with append

OutputStream os = new FileOutputStream(f, true);

If the append boolean is true which means it will append the new content with the old content.

instead of

OutputStream os = new FileOutputStream(f);

FileOutputStream has the default append method also. So use this to append the content with the old one.

newuser
  • 7,892
  • 2
  • 23
  • 33
  • please, what is the second boolean arameter for? – rmaik Dec 15 '14 at 08:51
  • It is the flag for "append", so if it is set to 'true', the new text will be added to the existing file, if it is set to 'false' the old contents are deleted before writing to the file. – Phiwa Dec 15 '14 at 08:57
0

Try this (please note the boolean passed to FileOutputStream):

File f = new File(SystemConfig.getSystConfigInstance("E"));
System.out.println(f.getAbsolutePath() + " name: " + f.getName());

OutputStream os = new FileOutputStream(f, true);  // <--- append = true
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write("new information");

For more info read the official doc of FileOutputStream:

FileOutputStream(File file, boolean append)

If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

Alex Gidan
  • 2,332
  • 14
  • 26
0
    File f = new File(SystemConfig.getSystConfigInstance("E"));;
    System.out.println(f.getAbsolutePath() + " name: " + f.getName());

    OutputStream os = new FileOutputStream(f,true);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
    bw.write("new information");    
    bw.close();

You have missed append flag for FileOutputStream. Then only it created as appendable. Otherwise it will like a read only.

Next , end of the file you need to close the writer. So that only it will flush entire data.

Siva Kumar
  • 1,990
  • 1
  • 12
  • 26