3 Replies Latest reply: Oct 2, 2006 12:20 AM by EJP RSS

    Overwrite file using BufferedWriter

      I am using BufferedWriter in my in following way:
      BufferedWrite out = new BufferedWriter(new FileWriter(filename));

      and then i'm writing to the file using out.write;

      but everytime run the program, the text gets appended to the file, while I want overwrite the existing file and then write to it.

      How do I do this??
        • 1. Re: Overwrite file using BufferedWriter
          You specify 'false' instead of 'true' to the 'append' parameter of new FileWriter(), or you leave it out, as per your example, which evidently isn't your real code.
          • 2. Re: Overwrite file using BufferedWriter
            thats helps me to understand a bit more, but my problem is still not over. Let me make it even clear this time.
            I have a GUI with a button. When I click the button, it is supposed to write to a file. So if I run the program for the first mouse click it will overwrite the file, but when i click on the mouse more than once in the same runtime, it appends to the file instead of overwrite on each click.
            The method that is called by the button has following contents:

            BufferedWriter out = null;
            File output_file = new File(output);
            out = new BufferedWriter(new FileWriter(output_file));

            out.write("Output this line");

            catch(IOException e){

            can you provide further insight now
            • 3. Re: Overwrite file using BufferedWriter
              Works for me. The file length is 16 every time out of 100 iterations.

              Are you sure this is the code being executed?