3 Replies Latest reply: Jun 18, 2008 7:16 AM by 800308 RSS



      I would like to know how to write in a file when accessing a method in a class several times with several values of a string. The question is how to create a BufferedWriter, write to it every time you access to the method and close it when finishing.

      This is what i have:
       private void writeFile() {
                         StringBuffer buf = getData();
                              // Create file                       
                              BufferedWriter out = new BufferedWriter( new FileWriter("out.txt")); 
                           }catch (Exception e){//Catch exception if any
                              System.err.println("Error: " + e.getMessage());
      (buf is an StringBuffer object that is returned )

      And the problem is that every time i access to writeFile it creates a new file with the new value of buf. However, what i actually need is to create a file with all values of the values that buf is getting.

      Result of code:
      if writeFile() has been called 3 times and getData returns these three values: asd adf afg
      result in the file would be last string (here afg) and what i want are the three strings.

      Hope you can help me.


        • 1. Re: BufferedWriter
          One of the constructors for FileWriter specifies that you want to append to the file. Use that constructor instead.
          • 2. Re: BufferedWriter

            The simple answer is to [open the file in append mode|http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html#FileWriter(java.lang.String,%20boolean)]... but this method "doesn't add value". I suspect that you may be better of redesigning to use a BufferedWriter directly in whatever loop is producing the data you wish to write.

            If you do wish to retain your current design then you could either:
            (a) pass the filename into this classes constructor; use it to initialise a Writer attribute; re-use that Writer in each call to writeFile; expose a close method and allways call it from the client code.
            (b) pass a Writer into the writeFile() method.

            Also you are "eating" your IOExceptions, so the caller has no way of knowing "it didn't work"... which is never a good idea.

            Also if out.write fails for any reason the file will be left unclosed. Generally close is called in a finally block... ergo
               * (re)writes the given content to the given filename
               * @param String content - the new contents of the fil
               * @param String filename - the name of the file to write.
              public static void write(String content, String filename) {
                try {
                  BufferedWriter out = null;
                  try {
                    out = new BufferedWriter(new FileWriter(filename));
                  } finally {
                } catch (IOException e) {
                  throw new RuntimeIOException(e.getMessage(), e);
            - The output file will allways be closed.
            - IOException is wrapped in a (unchecked) RuntimeIOException... debatably unwise!
            - No good for big files... only what fits comfortably in memory... say < 100 Mb

            Cheers. Keith,

            Edited by: corlettk on 18/06/2008 12:15 - Oops.
            • 3. Re: BufferedWriter
              Thanks! now it's working :-)