Click here to Skip to main content
16,022,538 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
why do we call the flush method of the binarywriter before closing it.what does the flush method actually do and is it necessary to call the flush method of binarywriter before closing it.
Posted

It clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

NOTE: All derived classes should override Flush to ensure that all buffered data is sent to the stream.
Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Cheers,
Edo
 
Share this answer
 
v2
Comments
fjdiewornncalwe 12-Feb-13 9:17am    
+5.
Joezer BH 13-Feb-13 2:15am    
Thank you Marcus
You call Flush when you want to write the content to the stream without closing it - so that is can be read by another process (assuming you don't have an exclusive lock on the object at the other end of the stream) - it is not normally necessary to call it for most streams (with the exception of some encrypted streams where flushing is important).
Calling Close on a stream will automatically Flush the stream anyway.
 
Share this answer
 
Comments
rtz87 12-Feb-13 8:24am    
not exactly clear to me can u be more detailed on this with a possible example.
fjdiewornncalwe 12-Feb-13 9:18am    
Why do you need an example. You simply call .Flush() on your stream, that is the code.
fjdiewornncalwe 12-Feb-13 9:17am    
+5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900