1

I understand that the BufferedWriter stores the information before I write in in a file before executing the act of writing it in a file using flush(), Append() etc..

I gather information from multiple sources, so currently what I am doing is looping in each source and appending it directly to the file each time, but what I'm trying to accomplish is to add all the information in the BufferedWriter and after finishing the loop, writing it to the file, how could that be done?

I am trying to improve performance by not flushing the data into the file so many times. The performance is issue because this might loop 1 million times.

Here is what I'm currently doing:

  1. Open BufferedWriter
  2. read data from a different source and storing in the buffer
  3. appending stored data in a text file(here the buffer is emptied)
  4. repeating steps 2.- and 3.- 50 times
  5. closing text file

Here is what I'm trying to do:

  1. Open BufferedWriter
  2. read data from a different source and storing in the buffer
  3. repeat step 2.- 50 times
  4. append all the data collected(the data gathered over the 50 loops)
  5. close the file

here is the code.

 for (int mainLoop = 0; mainLoop < 50; mainLoop++){

        try {
            BufferedWriter writer = writer = new BufferedWriter(new 
FileWriter
                    ("path to file in computer" + mainLoop + ".txt", true));


        for(int forloop = 0; forloop < 50; forloop++) {
            final Document pageHtml= 
Jsoup.connect("link to a page").get();


            Elements body = pageHtml.select("p");

            writer.append(System.getProperty("line.separator"));
            writer.append(System.getProperty("line.separator"));
            writer.append(body.text());

            System.out.println(forloop);
        }
        writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }continue;
    }
BLH-Maxx
  • 39
  • 2
  • 11
  • "how could that be done" .... with flush(). Not sure what you were expecting. – kumesana Sep 27 '18 at 13:31
  • Increase the size of the buffer so it doesn't have to write to the disk as often, and `flush()` after the loop (not during). – Andrew S Sep 27 '18 at 13:35
  • @AndrewS thats what i am trying to do but... how do i add more info into a buffer currently i am just appending the info i am gathering directly after getting it – BLH-Maxx Sep 27 '18 at 14:18
  • `append()` will use the internal buffer, and only write to disk when the buffer becomes full. – Andrew S Sep 27 '18 at 14:47
  • but... when i append the information into the file then i restart the loop and get new data to put in so i clear the buffer. what i am trying to get is to store more info into the buffer before i append it. – BLH-Maxx Sep 27 '18 at 15:00
  • Please [edit](https://stackoverflow.com/posts/52538003/edit) your question to clarify it by adding Java source code showing what you're currently doing in your loop. – DodgyCodeException Sep 28 '18 at 16:09
  • i have edited now – BLH-Maxx Sep 29 '18 at 06:46

2 Answers2

0

i am trying to improve performance by not flushing the data into de file so many times

Are you flushing the data manually after each write? Don't do that.

Otherwise, specify a larger size when you instantiate your BufferedWriter.

You have the option of using a StringBuilder to aggregate the output first. However, I assume you have more output than you want to store in memory.

Finally, is there really a performance cost?

===

The BufferedWriter will optimize the actual writes it performs. As long as you specify a large buffer size, e.g., 10,000, multiple small writes to the buffer will not cause a write until the buffer is full. I see a comment that you are "clearing" the buffer. Don't do that. Leave the BufferedWriter alone and let it do its thing.

If you are accumulating information and then, for some reason, discarding it, use a StringBuilder to accumulate and then write the StringBuild content to the Writer.

Steve11235
  • 2,849
  • 1
  • 17
  • 18
  • i edited the question to be more clear what i am trying to achieve and yes performance is a issue because of the number of loops that this might end doing and i am not flushing manually just a regular append – BLH-Maxx Sep 27 '18 at 14:09
  • 2
    @BLH-Maxx Just allocate a reasonably large buffer (but not too large!) and write into it. Don't call `flush()`, just close it when you're done. That is all there is to it. You are making this more complicate than necessary. – Mark Rotteveel Sep 28 '18 at 15:54
0

A buffered writer will flush when you instruct it to. And also any time the buffer becomes full. It can be tricky to determine when buffer becomes full. And really, you should not care. A buffered writer will improve performance regardless of precisely when it flushes. Instead, your output code should use BufferedWriter just like any other Writer.

I also see in your code that you repeatedly open and close the output file. You almost certainly don't need to do that. Instead open and close the file at a higher level in your program, so it remains open for each iteration.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • It does use a BufferedWriter and i use 2 kind of loops its a loop that runs a loop 50 times and i do need to open and close in the outer loop becuade its a new file but i dont open and close it in the inner loop. Do i realy make no sence? – BLH-Maxx Sep 29 '18 at 10:06