close
Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Using flush() before close()

As per the java docs, invoking close() on any java.io Streams automatically invokes flush(). But I have seen in lot of examples, even in production codes, developers have explicitly used flush() just before close(). In what conditions we need to use flush() just before close()?

Answer*

Draft saved
Draft discarded

Required fields are marked with *

Cancel
2
  • 1
    Both examples provided are essentially thin wrapper classes, so their flush and close implementations delegate to the underlying stream. I can't see how this alone could lead to any problems, as long as any "real" or buffering streams do flush when being closed. Commented Oct 7, 2020 at 9:40
  • However I realize that the behavior is not (and can't be) enforced by the javadoc of Closeable.close(). Commented Oct 7, 2020 at 9:44

lang-java