BufferedWriter

Hi,What is the difference between flush method and close method in BufferedWriter ?
[97 byte] By [samue-1a] at [2007-10-2 20:35:55]
# 1

> 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

tjacobs01a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 2
oh it's samue-1...I should have guessed
tjacobs01a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 3
I read the tuotrial and understood it.But is it necessary to write flush method for Buffer's sake all the time ?
samue-1a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 4
Ah I also make sure that I understood it correctly,I think that flush give permission to the stream to write until Buffer is full. Is it corect ?
samue-1a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 5
flush() empties the buffer
ejpa at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 6
I do not see any differences between flush() and close() method's functions, do you ?
samue-1a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

pbrockway2a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...
# 8
Thanks very much...
samue-1a at 2007-7-13 23:19:08 > top of Java-index,Java Essentials,Java Programming...