• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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() ?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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-
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic