participate


New To Java - BufferedWriter cannot append to file ....just overwrites
<<   Back to Forum  |   Give us Feedback
This topic has 5 replies on 1 page.
gforty
Posts:112
Registered: 11/8/04
BufferedWriter cannot append to file ....just overwrites   
Jan 24, 2006 8:43 AM

 
Hello all,
I have a java file that is an somewhat of a Event Handler.
I have a method inside that is called many times while program runs.
  public void writeToFile(String string)
  {
      try
      {
            BufferedWriter writer = new BufferedWriter(new FileWriter("foo.txt"));
            writer.append(string);
           // writer.write(string); tried this also.
            writer.flush();
            writer.close();
      }
      catch(IOException io)
            {
                io.printStackTrace();
            }
  }


Problem is that I only get one line written to foo.txt, and each time I run the program it overwrites that one line with another value.
Expected results are at least 4 lines would be written in foo.txt each time I run program.

Any ideas?
TIA!
 
sabre150
Posts:21,244
Registered: 10/24/97
Re: BufferedWriter cannot append to file ....just overwrites   
Jan 24, 2006 8:52 AM (reply 1 of 5)  (In reply to original post )

 
Construct a BufferedWriter from a FileOutputStream constructed so as to append the data i.e.

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(your file, true), fileEncoding_));
 
masuda1967
Posts:561
Registered: 8/30/00
Re: BufferedWriter cannot append to file ....just overwrites   
Jan 24, 2006 8:54 AM (reply 2 of 5)  (In reply to original post )

 
BufferedWriter writer = new BufferedWriter(new FileWriter("foo.txt"));

//
// change to
// FileWriter(filename, appendMode)
//

BufferedWriter writer = new BufferedWriter(new FileWriter("foo.txt", true));
 
gforty
Posts:112
Registered: 11/8/04
Re: BufferedWriter cannot append to file ....just overwrites   
Jan 24, 2006 9:01 AM (reply 3 of 5)  (In reply to #2 )

 
Thanks to you both!
I tried both suggestions and they both work.
 
gforty
Posts:112
Registered: 11/8/04
Re: BufferedWriter cannot append to file ....just overwrites   
Jan 24, 2006 9:06 AM (reply 4 of 5)  (In reply to #2 )

 
I dont suppose that you know if it is possible to append to the top (beginning)of the file?
 
FuzzyOnion
Posts:504
Registered: 7/5/05
Re: BufferedWriter cannot append to file ....just overwrites   
Jan 24, 2006 9:11 AM (reply 5 of 5)  (In reply to #4 )

 
I dont suppose that you know if it is possible to
append to the top (beginning)of the file?

That would be inserting. To do that, you could store the whole file, then rewrite the file, putting the old data after your new data.

Or you could create a new file, write the new data to it, then read the old file and write the data there to the new file. After that, delete the old file and rename the new file.
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 26
  • Guests : 122

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums