Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to txt file with StreamWriter and FileStream

Tags:

I ran into something interesting when using a StreamWriter with a FileStream to append text to an existing file in .NET 4.5 (haven't tried any older frameworks). I tried two ways, one worked and one didn't. I'm wondering what the difference between the two is.

Both methods contained the following code at the top

if (!File.Exists(filepath))     using (File.Create(filepath)); 

I have the creation in a using statement because I've found through personal experience that it's the best way to ensure that the application fully closes the file.

Non-Working Method:

using (FileStream f = new FileStream(filepath, FileMode.Append,FileAccess.Write))     (new StreamWriter(f)).WriteLine("somestring"); 

With this method nothing ends up being appended to the file.

Working Method:

using (FileStream f = new FileStream(filepath, FileMode.Append,FileAccess.Write))     using (StreamWriter s = new StreamWriter(f))         s.WriteLine("somestring"); 

I've done a bit of Googling, without quite knowing what to search for, and haven't found anything informative. So, why is it that the anonymous StreamWriter fails where the (non-anonymous? named?) StreamWriter works?

like image 675
Leon Newswanger Avatar asked Feb 02 '13 01:02

Leon Newswanger


People also ask

What is the difference between FileStream and StreamWriter?

Specifically, a FileStream exists to perform reads and writes to the file system. Most streams are pretty low-level in their usage, and deal with data as bytes. A StreamWriter is a wrapper for a Stream that simplifies using that stream to output plain text.

Will StreamWriter create a file?

The first step to creating a new text file is the instantiation of a StreamWriter object. The most basic constructor for StreamWriter accepts a single parameter containing the path of the file to work with. If the file does not exist, it will be created. If it does exist, the old file will be overwritten.

Can be used to write a stream of text to a file?

C# StreamWriter The StreamWriter class in C# is used for writing characters to a stream. It uses the TextWriter class as a base class and provides the overload methods for writing data into a file. The StreamWriter is mainly used for writing multiple characters of data into a file.


2 Answers

It sounds like you did not flush the stream.

http://msdn.microsoft.com/en-us/library/system.io.stream.flush.aspx

It looks like StreamWriter writes to a buffer before writing to the final destination, in this case, the file. You may also be able to set the AutoFlush property and not have to explicitly flush it.

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush.aspx

To answer your question, when you use the "using" block, it calls dispose on the StreamWriter, which must in turn call Flush.

like image 138
Phillip Scott Givens Avatar answered Sep 19 '22 18:09

Phillip Scott Givens


The difference between the two code snippets is the use of using. The using statement disposes the object at the end of the block.

A StreamWriter buffers data before writing it to the underlying stream. Disposing the StreamWriter flushes the buffer. If you don't flush the buffer, nothing gets written.

From MSDN:

You must call Close to ensure that all data is correctly written out to the underlying stream.

See also: When should I use “using” blocks in C#?

like image 28
dtb Avatar answered Sep 19 '22 18:09

dtb