Results 1 to 6 of 6
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Problem with BufferedWriter

    Hi everyone,

    How do I append some text to a file? I'm using the BufferedWriter class. The main commands I'm running are

    writer.append ("Stuff to append");
    writer.close;

    However, when this program is runs, it deletes all of the current text and simply writes the "Stuff to append" line shown above. How do I simply append a string to the end of a file (or beginning), without deleting everything?

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: Problem with BufferedWriter

    Are you using a filewriter?

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Problem with BufferedWriter

    Which Writer do you use and how do you instantiate it?
    Look at the FileWriter class and the constructor which has a boolean parameter "append" :D
    FileWriter (Java Platform SE 6)

  4. #4
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Problem with BufferedWriter

    Woops! Let me show how it was instantiated; it was done with FileWRiter.

    File practice = new File("C:\\Users\\salvador.salazar\\Documents\\prac tice.txt");
    BufferedWriter writer = new BufferedWriter(new FileWriter(practice));

    for (int i = 0; i < 10; i++){
    writer.append("First Item" + i + ",More Stuff" + ",Nope");
    writer.newLine();
    }

    writer.append("More Stuff to Add");
    writer.newLine();
    writer.close();

    Again, when the program runs; it simply deletes the current file before writing.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Problem with BufferedWriter

    You missed what was said in post #3

  6. #6
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Problem with BufferedWriter

    You are very correct Norm. Thanks eRaaaam, that answers my question!

Similar Threads

  1. BufferedWriter Problem
    By dailywalker in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 10:07 PM
  2. Nullpointer Exception with BufferedWriter?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 12:08 AM
  3. BufferedWriter, FileWriter
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 02:35 AM
  4. BufferedWriter
    By Karthika in forum New To Java
    Replies: 6
    Last Post: 04-22-2010, 09:23 AM
  5. Replies: 0
    Last Post: 06-26-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •