Forums Register Login

Function of close() and flush() methods..

+Pie Number of slices to send: Send
My question is:

1> Do close() and flush() performs the same function??
2> Why their is no output when i do not write the pw.close() or pw.flush() ?
+Pie Number of slices to send: Send
The flush method only flushes any data in the buffer which is not written to the underlying stream. For example if you are writing to a file and you write "abc" to it, then the value might not be immediately written to the file, it may be stored in a buffer for writing to the file. Flush makes sure that there is nothing waiting to be written to the file (this is the same reason you don't get any output if you don't use flush). close method flushes the stream and then close it so you cannot write anything else to the stream...
+Pie Number of slices to send: Send
Oh It means close performs both the function of flushing and then closing.
That means close() will save writing flush() if we want to close the file directly
+Pie Number of slices to send: Send
 

Oh It means close performs both the function of flushing and then closing.
That means close() will save writing flush() if we want to close the file directly



I dont think close() operation will first work as flush() works and then close the stream.

I think it just close the output stream. A closed stream can not perform any output operations and can not be reopend.

Correct me if I am wrong somewhere
+Pie Number of slices to send: Send
Hi Brij,
I also checked by writing on close() method then also it write the data into the given file. It means it is also performing some sort of flush() function before closing
+Pie Number of slices to send: Send
 


Hi Brij,
I also checked by writing on close() method then also it write the data into the given file. It means it is also performing some sort of flush() function before closing



No close dont perform any flush operation.
Nothing of this sort has been written in JLS about close function.

flush operation make sure that if there is anything in the buffer, it gets flushed to the file.
But this does not mean if we will not use flush() function, then content will not get written to the file.

Need correction, if my understanding is wrong.
+Pie Number of slices to send: Send
Hi all,

From Java6 Writer javadoc about close() method:


Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.



The flush() method ensures stream flushing.

Practice shows that usually stuff gets written to file with just close() method. But it's always recommended and encouraged to call flush before closing the stream!
+Pie Number of slices to send: Send
Java 5 docs also says the same
JLS

Closes this output stream and releases any system resources associated with the stream.
The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.



Thanks Innar for correction
+Pie Number of slices to send: Send
AFAICT the following is true.

On Windows, if you don't do a "flush" on a file descriptor, before closing it, then your data might stay in some (OS) write queue and not be written out temporarily (it will get there eventually, but might take awhile). For instance if you write a file (windows only, again) then close it, then turn around and read it again, it might not (yet) have all the data. Odd, but true. So it's more of a safety measure to call a flush before a close, but may have some utility. That being said my experience is just from the ruby runtime: http://bugs.ruby-lang.org/issues/show/776 and may not hold true for the JVM.
Cheers!
-roger-
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 17655 times.
Similar Threads
Use of File Class...
working with files and directories
close() method; I/O class chaining
writing file
readLine() and read() functionality
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:14:24.