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

4

75

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

When my data wasn't being written correctly to a file, I added both Flush() and Close() to the end of my code. However, I realized that adding either Flush() or Close() allowed the data to be written correctly.

I wasn't able to pick up on exactly what each of these methods does when I read the MSDN docs; I only figured out that one or the other is necessary to ensure data is written correctly. Any further explanation would be much appreciated.


Where s is a string to be written, here's what my code looks like currently:

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

Based on feedback from the answers, I've rewritten my code in a using block, which implements IDisposable and will automatically take care of writing the stream to the file when the object is disposed:

using (StreamWriter sw = File.CreateText("TextOutput.txt"))
{
    sw.Write(s);
}
Semiotics answered 10/3, 2010 at 15:13 Comment(1)
Looks like StreamWriter is the one that implements iDisposable not the using blockMckenzie
B
136

StreamWriter.Flush() can be called any time you need to clear the buffer, and the stream will remain open.

StreamWriter.Close() is for closing the stream, at which point the buffer is also flushed.

But you shouldn't really need to call either of these. Any time I see a .Close() in code I take that as a code smell, because it usually means an unexpected exception could cause the resource to be left open. What you should do, is create your StreamWriter variable in a using block, like this:

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.
Bouchard answered 10/3, 2010 at 15:13 Comment(14)
This because Stream.Dispose calls Close()Gorgias
to be clear, enapsulating a StreamWriter object in a using (or try/catch) block will force the object to be closed?Semiotics
@Ben - encapsulating it in a try/catch will not force it to close. Encapsulating your .Close() call in a finally block or using block will, and a using block is so much more convenient.Bouchard
obviously this depends on the needs of the app in question, but my impression is if you had an exception writing to a file you would want to handle it in some way (notify user, etc.)...I actually can't think of any scenario where I would use using for file i/o...am I missing something?Surplus
@Surplus - using (var writer = new StreamWriter("file.txt")) try { //writer.writestuff(); :) }catch(...) { }Bouchard
"using" cannot be used when you are using the stream writer object to write at different places in the code. In that case, you have to close the stream manually.Washroom
"But you shouldn't really need to call either of these"... not true with flush(). You may very well need to call flush() so you don't run out of memory.Phatic
@Phatic I have written out 100+ GB files with StreamWriter, and never seen it use much memory. It's internal buffer should be very small, and shouldn't have any noticeable impact on memory. It auto-flushes once the buffer fills up.Kenn
There are times when an explicit .Flush() is useful... but they are rare.Bouchard
What scenarios is an explicit Flush() useful?Rheinlander
@Rheinlander Streams that stay open and active over longer periods of time, where there are separate readers and writers. In those cases, you flush when you finish writing a batch, to be sure it will be available to readers.Bouchard
According to @davidfowl at MS, when using WriteAsync() you should, .."Always call FlushAsync() on StreamWriter(s) or Stream(s)" The guidance link - @Joel Coehoorn - do you agree and if so would you consider updating your answer? I realized you answered 11 years ago :)Qatar
@Qatar This answer pre-dates the Async() methods, and I haven't personally had the chance to explore them.Bouchard
@Qatar David does qualifies his just as dogmatic statement with "Either use the asynchronous DisposeAsync method (for example via await using) or call FlushAsync before calling Dispose"Magdalenamagdalene
F
17

StreamWriter.Flush() will flush anything in the Stream out to the file. This can be done in the middle of using the Stream and you can continue to write.

StreamWriter.Close() closes the Stream for writing. This includes Flushing the Stream one last time.

There's a better way to do things though. Since StreamWriter implements IDisposable, you can wrap the 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 Flush and Close the Stream for you.

Farias answered 10/3, 2010 at 15:14 Comment(1)
Flush() does not write anything to the physical file, only to the underlying stream.Roybal
S
9

I had a case where I was writing a very long string to a StreamWriter with an underlying MemoryStream. The MemoryStream was being consumed by something else before the writer and the stream were disposed.

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

With really long strings the end of the string was being truncated. This was resolved by calling flush before the stream was used. Alternatively I could have set AutoFlush to true.

Solus answered 29/6, 2010 at 15:41 Comment(0)
C
1

From MSDN:

Flush: Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

Close: Closes the current StreamWriter object and the underlying stream.

Catania answered 10/3, 2010 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.