Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

flush()  RSS feed

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does flush method do in BufferedWriter ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A BufferedWriter always has an "underlying Writer" -- i.e., it always sends its output to some other Writer, like a FileWriter. The flush() call copies all the data out of the BufferedWriter's buffer and sends it to that underlying Writer. Closing the BufferedWriter will do the same thing.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry but it is a little bit messy for me.
Would you mind being more clear ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what a "buffer" is, and understand the idea of "buffering"?
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it increases the efficiency by having readLine method...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
Yes, it increases the efficiency by having readLine method...


Ummmm.... no, not so much.

First, you probably realize that reading or writing data from a file or network connection is generally a slow operation compared to reading from memory. It's also true in general that reading or writing a thousand bytes from a disk takes only a little more time than reading or writing one byte. So if you're going to read or write a thousand bytes, it's much faster to do them all at once than to do them one at a time.

A "buffer" is just an array or something similar for storing data. A BufferedWriter stores the data you give to it in a buffer until it has a lot of data, and then sends it to the underlying Writer all at once. So, for example, let's say you create a BufferedWriter like this:

BufferedWriter w = new BufferedWriter(new FileWriter("myfile.txt"));

That FileWriter is the "underlying Writer" I've been talking about. Now let's say you do this:

w.write("Hello");
w.write(" ");
w.write("World");
w.flush();

Each time you call write(), the BufferedWriter stores those Strings in its buffer. Then when you call flush() it calls

write("Hello World");

on the FileWriter, just once. Altogether, this is faster than if you called write() on the FileWriter three separate times.

Do you understand now?
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, thanks very much for this extremely explanatory reply.
Now, I think that I kept the idea.

Again thanks very much for this.

Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!