Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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() ?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic