Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedWriter cannot append to file ....just overwrites

807598Jan 24 2006 — edited Jan 24 2006
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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 21 2006
Added on Jan 24 2006
5 comments
396 views