What is the difference between StreamWriter.Flush () and StreamWriter.Close ()?

What is the difference in functionality between StreamWriter.Flush() and StreamWriter.Close() ?

When my data was not written to the file correctly, I added both ends of Flush() and Close() to the end of my code. However, I realized that adding either Flush() or Close() allowed me to write the data correctly.

I was not able to understand what each of these methods does when I read MSDN documents; I just found out that one or the other is necessary to ensure that the data is written correctly. Any further explanation would be greatly appreciated.




Where s is the line to be written, here is what my code currently looks like:

  StreamWriter sw = File.CreateText("TextOutput.txt"); sw.Write(s); sw.Flush(); sw.Close(); 



Based on the feedback from the responses, I rewrote my code in the using block, which implements IDisposable and will automatically take care of writing the stream to the file when placing the object:

  using (StreamWriter sw = File.CreateText("TextOutput.txt")) { sw.Write(s); } 
+41
c # file-io
Mar 10 '10 at 15:13
source share
5 answers

StreamWriter.Flush() can be called anytime you need to clear the buffer and the stream remains open.

StreamWriter.Close() intended to close the stream, and at this point the buffer is also cleared.

But you really do not need to call any of them. Every time I see .Close() code, I perceive it as a smell of code, because usually it means that an unexpected exception can lead to the resource remaining open. What you should do is create a StreamWriter variable in the usage block, for example:

 using (var writer = new StreamWriter("somefilepath.txt")) { // write a bunch of stuff here } // the streamwriter WILL be closed and flushed here, even if an exception is thrown. 
+81
Mar 10 '10 at 15:13
source share

StreamWriter.Flush() will dump anything in the stream to a file. This can be done in the middle of using Stream, and you can continue to write.

StreamWriter.Close() closes the stream for recording. This includes Flushing the Stream for the last time.

There is a better way to do something. Since StreamWriter implements IDisposable , you can wrap StreamWriter in a using block.

 using(StreamWriter sw = new StreamWriter(stream)) { // Work with things here } 

After the using block, Dispose ... will be called, which will hide and close the stream for you.

+11
Mar 10
source share

I had a case where I wrote a very long line in StreamWriter with a basic MemoryStream. A MemoryStream was consumed by something else before the writer and stream were deleted.

 using (var memoryStream = new MemoryStream()) using (var streamWriter = new StreamWriter(memoryStream , Encoding.UTF8)) { streamWriter.Write(someRealyLongStringValue); DoSomethingWithTheStream(memoryStream ); } 

With very long lines, the end of the line was truncated. This was resolved by calling flush before the stream was used. Alternatively, I could set AutoFlush to true.

+4
Jun 29 '10 at 15:41
source share

Reset forces to write a buffer to disk. Close - closes the stream, and also performs an internal cleaning operation

+1
Mar 10 '10 at 15:15
source share

From MSDN:

Flush: flushes all buffers for the current author and forces writing buffer data to the base stream.

Close: closes the current StreamWriter and the underlying stream.

0
Mar 10
source share



All Articles