4

I have the following code:

CSVmaker(LinkedList data) {
    String [] myLines = makeStrings(data);
  //  for (int k = 0; k<myLines.length; k++)
  //  System.out.println(myLines[]);




    this.file = new File("rawdata.csv");
        try {
            BufferedWriter buff = new BufferedWriter(new FileWriter(file));
            for (int i = 0; i<myLines.length; i++){
                buff.write(myLines[i]);
                buff.newLine();
                System.out.println("done");
            }
        } catch (IOException ex) {
          System.out.println("except");
        }



}

No, I checked for the contents of myLines, these are correct.

Also, I get the print which prints "done" just as often as I should. The csv is created.

However, if I open it manually, it is empty.

What can be the reason for this?

newnewbie
  • 993
  • 2
  • 11
  • 26
  • 2
    don't you need to `buff.close()` ? – DevZer0 Sep 02 '13 at 13:04
  • 1
    The whole point of a buffer is to buffer the data until it is full or you tell it you want to write. You didn't flush() or close() the stream so the data never leaves the buffer. – Peter Lawrey Sep 02 '13 at 13:08
  • possible duplicate of [BufferedWriter won't write all of the data to a file](http://stackoverflow.com/questions/11660116/bufferedwriter-wont-write-all-of-the-data-to-a-file) – Val Sep 02 '13 at 14:01
  • possible duplicate of [BufferedWriter not writing everything to its output file](http://stackoverflow.com/questions/13426142/bufferedwriter-not-writing-everything-to-its-output-file) – Raedwald Jan 03 '15 at 11:21

4 Answers4

14

You never flush the buffer, or close the BufferedWriter.

After the for loop, make the following calls:

buff.flush();
buff.close();

Even with other resources, closing them when done is a good idea.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 4
    The close should actually be done in a finally block, because the file descripto should be closed even if any exception happens in the loop. If Java 7 is used, the try-with-resources statement should be preferred. – JB Nizet Sep 02 '13 at 13:06
  • @JBNizet Yes, I'm keeping it simple for clarity for the OP. – nanofarad Sep 02 '13 at 13:08
  • Wow, 2 correct answers after my taking just a 10 minutes shower. As I suppose I cannot vote for 2 I simply vote up who was first if that is ok. Did not yet manage to put in a finally though - If I do I get "cannot find variable buff" – newnewbie Sep 02 '13 at 13:18
  • Ran into an issue where certain string was being shown inside the file without using flush() and some were not. Upon investigating a bit further I found out that bufferwriter outputs to a file without flush() prompt if the size of the string is greater than the internal limit defined. – Sadiq Ali Jul 12 '17 at 12:16
  • `flush()` is redundant before `close()`. – Kayaman Nov 19 '17 at 14:21
4

You have to close() the stream after use.
Call buff.close() after write loop; BufferedWriter will flush data to file at close.

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
2

Though the question is answered . I would like to add how buffer works.

whenever you try to write to a file using buffer,whatever you write gets added to the buffer. When the buffer is full the contents are written to the file . This way we are reducing the number of hits to the hard-drive hence improving the efficency.

If we want to forcefully write to a file without the buffer getting full , we use flush() method.

Pankaj
  • 14,638
  • 3
  • 17
  • 23
0

Starting with Java 8, one would simply do it with a try with resources, which automatically closes the BufferedWriter. Also see the usage of the new class Files

try (BufferedWriter writer = Files.newBufferedWriter(somePath, yourCharset)){
    writer.write(output);
}
D. Spreuer
  • 47
  • 8