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...
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.
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.
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop