By Venkat

2010-02-26 07:35:42 8 Comments

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


tell me some suggestions.


@user3004437 2013-11-18 11:10:21

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

@user207421 2015-05-19 02:29:01

Only in the case of BufferedOutputStream, BufferedWriter, ObjectOutputStream, and the PrintXXX classes. The flush() method of any other stream or Writer does nothing.

@mr5 2015-10-21 02:38:14

@EJP Any references to backup your statements? It would be helpful for us newbies :)

@user207421 2016-06-09 11:40:55

@mr5 It is the poster who needs to back up his statements, not me, but everything I've said is stated clearly in the Javadoc

@Biman Tripathy 2012-11-01 20:44:12

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()

@John Henckel 2014-05-27 14:45:42

Calling flush just before close is redundant, right? it is the same as simply calling close.

@user207421 2015-05-19 02:29:46

@BimanTripathy I can say that with 100% assurance. It is guaranteed by the Javadoc of FilterOutputStream and FilterWriter.

@Biman Tripathy 2016-11-09 06:47:50

yes, calling flush before close is redundant.

@Semih Okan Pehlivan 2016-09-21 09:03:24

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:


@codaddict 2010-02-26 07:40:00

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.

@Oleksandr 2013-06-06 11:39:56

@rayryeng 2014-06-02 16:01:46

+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.

@tnishada 2016-06-30 08:54:27

Is the flush method only used with buffered streams ? is there any advantage of using it with other streams (eg outputstream) as well ?

@Jonathan Komar 2019-05-23 13:13:48

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.

@vijaya kumar 2015-04-04 15:56:52

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).

@Silvio Donnini 2010-02-26 07:45:09

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.

@user207421 2016-06-09 11:22:01

This is fantasy. Streams are not accessed by any magical threads; only those created by the application.

Related Questions

Sponsored Content

24 Answered Questions

27 Answered Questions

33 Answered Questions

[SOLVED] What's the simplest way to print a Java array?

  • 2009-01-03 20:39:39
  • Alex Spurling
  • 2127271 View
  • 1826 Score
  • 33 Answer
  • Tags:   java arrays printing

16 Answered Questions

[SOLVED] What is a JavaBean exactly?

84 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

23 Answered Questions

[SOLVED] What is a daemon thread in Java?

23 Answered Questions

[SOLVED] What exactly is Apache Camel?

35 Answered Questions

[SOLVED] How do I break out of nested loops in Java?

  • 2009-05-20 09:07:43
  • boutta
  • 1079882 View
  • 1733 Score
  • 35 Answer
  • Tags:   java loops

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

30 Answered Questions

[SOLVED] How to avoid Java code in JSP files?

  • 2010-07-05 07:24:06
  • chmoelders
  • 275212 View
  • 1640 Score
  • 30 Answer
  • Tags:   java jsp scriptlet

Sponsored Content