C# 使用 StreamWriter 和 FileStream 写入 txt 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14657643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing to txt file with StreamWriter and FileStream
提问by Leon Newswanger
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.
在 .NET 4.5 中使用StreamWriter
with aFileStream
将文本附加到现有文件时遇到了一些有趣的事情(还没有尝试过任何旧框架)。我尝试了两种方法,一种有效,一种无效。我想知道两者之间有什么区别。
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.
我在using
声明中进行了创建,因为我通过个人经验发现这是确保应用程序完全关闭文件的最佳方式。
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?
我做了一些谷歌搜索,但不知道要搜索什么,也没有找到任何有用的信息。那么,为什么匿名StreamWriter
在(非匿名?命名?)StreamWriter
工作的地方失败了?
采纳答案by Phillip Scott Givens
It sounds like you did not flush the stream.
听起来您没有刷新流。
http://msdn.microsoft.com/en-us/library/system.io.stream.flush.aspx
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.
看起来 StreamWriter 在写入最终目标(在本例中为文件)之前写入缓冲区。您也可以设置 AutoFlush 属性,而不必显式刷新它。
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush.aspx
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.
为了回答您的问题,当您使用“using”块时,它会在 StreamWriter 上调用 dispose,而后者必须依次调用 Flush。
回答by dtb
The difference between the two code snippets is the use of using
. The using
statementdisposes the object at the end of the block.
两个代码片段之间的区别在于使用using
. 该using
语句在块的末尾处理对象。
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.
AStreamWriter
在将数据写入底层流之前缓冲数据。处理StreamWriter
刷新缓冲区。如果您不刷新缓冲区,则不会写入任何内容。
From MSDN:
从MSDN:
You must call Close to ensure that all data is correctly written out to the underlying stream.
您必须调用 Close 以确保所有数据都正确写出到底层流。
See also: When should I use “using” blocks in C#?