File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Function of close() and flush() methods.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Function of close() and flush() methods.. " Watch "Function of close() and flush() methods.. " New topic
Author

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

Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
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() ?


See Dreams With Open Eyes And Make It Real
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
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
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
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
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
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
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

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.
Innar Made
Greenhorn

Joined: May 12, 2009
Posts: 17
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!
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
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
Roger Pack
Greenhorn

Joined: Aug 24, 2011
Posts: 4
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-
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Function of close() and flush() methods..