What is the purpose of flush() in Java streams?
Asked Answered
J

7

157

In Java, flush() method is used in streams. But I don't understand what are all the purpose of using this method?

fin.flush();

tell me some suggestions.

Joinder answered 26/2, 2010 at 7:35 Comment(0)
J
129

From the docs of the flush method:

Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

The buffering is mainly done to improve the I/O performance. More on this can be read from this article: Tuning Java I/O Performance.

Jenkins answered 26/2, 2010 at 7:40 Comment(4)
Fresh link to Tuning Java I/O Performance oracle.com/technetwork/articles/javase/perftuning-137844.htmlLeftover
+1 Thank you. I couldn't figure out why my bytes weren't sent out, and it's because I had to flush my buffers.Official
Is the flush method only used with buffered streams ? is there any advantage of using it with other streams (eg outputstream) as well ?Saddlecloth
Maybe the word 'flush' should not be in its definition. Recursive definitions are a bad idea for understanding new things. I am aware that you did not write the definition.Amply
D
39

When you write data to a stream, it is not written immediately, and it is buffered. So use flush() when you need to be sure that all your data from buffer is written.

We need to be sure that all the writes are completed before we close the stream, and that is why flush() is called in file/buffered writer's close().

But if you have a requirement that all your writes be saved anytime before you close the stream, use flush().

Delao answered 1/11, 2012 at 20:44 Comment(2)
Calling flush just before close is redundant, right? it is the same as simply calling close.Superorder
@BimanTripathy I can say that with 100% assurance. It is guaranteed by the Javadoc of FilterOutputStream and FilterWriter.Peccavi
S
31

When we give any command, the streams of that command are stored in the memory location called buffer(a temporary memory location) in our computer. When all the temporary memory location is full then we use flush(), which flushes all the streams of data and executes them completely and gives a new space to new streams in buffer temporary location. -Hope you will understand

Sidesman answered 18/11, 2013 at 11:10 Comment(5)
Only in the case of BufferedOutputStream, BufferedWriter, ObjectOutputStream, and the PrintXXX classes. The flush() method of any other stream or Writer does nothing.Peccavi
@EJP Any references to backup your statements? It would be helpful for us newbies :)Unhinge
@Unhinge It is the poster who needs to back up his statements, not me, but everything I've said is stated clearly in the JavadocPeccavi
Invoking flush() explicitly is not required for a BufferedWriter, as long as you close() the writer. close() executes a flush itself.Linguiform
Note close() calls flush(), so calling both is redundant when using FileWriter without buffer.Linguiform
K
8

If the buffer is full, all strings that is buffered on it, they will be saved onto the disk. Buffers is used for avoiding from Big Deals! and overhead.

In BufferedWriter class that is placed in java libs, there is a one line like:

private static int defaultCharBufferSize = 8192;

If you do want to send data before the buffer is full, you do have control. Just Flush It. Calls to writer.flush() say, "send whatever's in the buffer, now!

reference book: https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208

pages:453

Kamp answered 21/9, 2016 at 9:3 Comment(0)
K
7

In addition to other good answers here, this explanation made it very clear for me:

A buffer is a portion in memory that is used to store a stream of data (characters). These characters sometimes will only get sent to an output device (e.g. monitor) when the buffer is full or meets a certain number of characters. This can cause your system to lag if you just have a few characters to send to an output device. The flush() method will immediately flush the contents of the buffer to the output stream.

Source: https://www.youtube.com/watch?v=MjK3dZTc0Lg

Karilynn answered 18/12, 2020 at 9:26 Comment(0)
A
4

Streams are often accessed by threads that periodically empty their content and, for example, display it on the screen, send it to a socket or write it to a file. This is done for performance reasons. Flushing an output stream means that you want to stop, wait for the content of the stream to be completely transferred to its destination, and then resume execution with the stream empty and the content sent.

Arboreous answered 26/2, 2010 at 7:45 Comment(1)
This is fantasy. Streams are not accessed by any magical threads; only those created by the application.Peccavi
E
3

For performance issue, first data is to be written into Buffer. When buffer get full then data is written to output (File,console etc.). When buffer is partially filled and you want to send it to output(file,console) then you need to call flush() method manually in order to write partially filled buffer to output(file,console).

Expend answered 4/4, 2015 at 15:56 Comment(1)
This is not true. FileOutputStream.flush() is an empty no-op.Rubellite

© 2022 - 2024 — McMap. All rights reserved.