> What is the difference between flush method and close
> method in BufferedWriter ?
The best answer is given in the javadocs. Anything that these methods do outside of what's stated in the docs is effectively a side effect that may or may not change based on version/platform/jvm producer
flush() and close() are provided by classes which implement Flushable
and Closeable respectively.
The docs for these interfaces say that flush() "Flushes this stream by
writing any buffered output to the underlying stream." and that close()
"Closes this stream and releases any system resources associated
with it.".
On the face of it they have little in common: one empties the stream,
the other disconnects it.
Classes which implement Flushable and Closeable can - and do - go
further. A PrintStream's flush() "is done by writing any buffered output
bytes to the underlying output stream and then flushing that stream. "
In other words the flush is "recursive".
PrintStream's close() "is done by flushing the stream and then closing
the underlying output stream."Again the behaviour is that it's closed
"all the way down", but PrintStream promises to flush before it
disconnects from the plumbing (very wise).
Oddly, although you can trust a PrintStream to flush before it closes
(so there is no reason to explicitly flush), PrintWriter makes no such
promise.