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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:37:03  来源:igfitidea点击:

Writing to txt file with StreamWriter and FileStream

c#

提问by Leon Newswanger

I ran into something interesting when using a StreamWriterwith a FileStreamto 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 中使用StreamWriterwith aFileStream将文本附加到现有文件时遇到了一些有趣的事情(还没有尝试过任何旧框架)。我尝试了两种方法,一种有效,一种无效。我想知道两者之间有什么区别。

Both methods contained the following code at the top

这两种方法的顶部都包含以下代码

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

I have the creation in a usingstatement 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 StreamWriterfails where the (non-anonymous? named?) StreamWriterworks?

我做了一些谷歌搜索,但不知道要搜索什么,也没有找到任何有用的信息。那么,为什么匿名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 usingstatementdisposes the object at the end of the block.

两个代码片段之间的区别在于使用using. using语句在块的末尾处理对象。

A StreamWriterbuffers data before writing it to the underlying stream. Disposing the StreamWriterflushes 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#?

另请参阅:何时应该在 C# 中使用“using”块?