StreamWriter.Flush()还会调用FileStream.Flush()吗? [英] Will StreamWriter.Flush() also call FileStream.Flush()?

查看:68
本文介绍了StreamWriter.Flush()还会调用FileStream.Flush()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个StreamWriter,其基础流是 FileStream .以下代码将确保 FileStream 还将其缓冲区刷新到文件系统上的实际文件中,还是我需要在上显式调用 Flush()FileStream ?

  using(var fs = new FileStream("blabla",FileMode.Append)){使用(var sw = new StreamWriter(fs)){sw.WriteLine(你好,我想被冲洗.");sw.Flush();//我还需要它刷新到文件上,而不只是刷新到FileStream上}} 

按照 MSDN ,"刷新流不会刷新其基础编码器,除非您显式调用Flush或Close ,但是我不知道FileStream是否可以被视为基础编码器".

此外,如果我未指定FileOptions.WriteThrough,即使程序在关闭两个流之前崩溃,我也保证操作系统最终会将刷新的行写入磁盘上(例如,假设没有使用{} 块,仅调用 Flush())吗?

在我的场景中,我需要保持流打开(用于记录),因此我不能使用 using {} 块,但是我想确保即使将数据始终写入磁盘,即使程序崩溃.如果断电并且操作系统没有刷新到磁盘上,我可以承受丢失数据的负担,但是否则,即使我从未正确调用流中的 Close(),我也需要操作系统最终刷新.

解决方案

是的,在 StreamWriter 上调用 Flush 将导致基础流为 Flush .4.5版本调用一个私有的 Flush(bool,bool)函数,该函数的结尾为:

  if(flushStream){this.stream.Flush();} 

其中 flushStream 是第一个参数, this.stream StreamWriter 所构建的流,并在 Flush() Flush(true,true).


(答案的较早部分-我在回答时非常round回.将答案中最相关的部分移到顶部)

在我能找到它的任何地方,它都没有在文档中明确说明,但应该假定通过将另一个流传递给另一个流而构造的任何流类都拥有"该流的所有权(除非另外明确指出)./p>

也就是说,一旦使用 fs 构建了 StreamWriter ,就不要对 fs 进行任何直接操作.


您从MSDN引用的部分与后面的句子有关:

这允许编码器保持其状态(部分字符),以便可以正确编码下一个字符块.这种情况会影响UTF8和UTF7,在UTF8和UTF7中,某些字符只能在编码器收到相邻的一个或多个字符之后才能进行编码.

也就是说,您可能已经将数据传递给了 Write ,因此您已经给它提供了一些Unicode替代,但不是完整的字符. Flush 不会将这些替代内容写入流中.只要您始终将格式正确(完整)的字符串传递给 Write ,就不必为此担心.


I have a StreamWriter which underlying stream is a FileStream. Will the following code guarantee that the FileStream also flushes its buffer into the actual file on the file system, or do I need to explicitly call Flush() on the FileStream?

using (var fs = new FileStream("blabla", FileMode.Append)) {
    using (var sw = new StreamWriter(fs)) {
        sw.WriteLine("Hello, I want to be flushed.");
        sw.Flush(); //I need this to also flush onto the file, not just to the FileStream
    }
}

As per MSDN, "Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close", but I do not know if a FileStream can be considered an "underlying encoder".

Also, if I don't specify FileOptions.WriteThrough, am I guaranteed that the OS will eventually write the flushed line onto the disk even if the program crashes before the two streams have been closed (assuming for example no using {} blocks, only a call to Flush())?

In my scenario I need to leave the stream open (for logging) so I cannot use using {} blocks, but I would like to make sure data will always be written to the disk even if the program crashes. I can afford to lose data if there is a power shutdown and the OS has not flushed onto the disk, but otherwise I need the OS to eventually flush even if I never properly call Close() on the stream.

解决方案

Yes, calling Flush on StreamWriter will cause the underlying stream to be Flushed. The 4.5 version calls a private Flush(bool,bool) function, which ends with:

if (flushStream)
{
    this.stream.Flush();
}

Where flushStream is the first parameter, this.stream is the stream that the StreamWriter was constructed on, and the call in Flush() is Flush(true,true).


(Older parts of answer - I was being very roundabout in answering. Moved most relevant part of answer to top)

It's not explicitly spelled out in the documentation anywhere I can find it, but any stream class that is constructed by passing it another stream should be assumed to "take ownership" of that stream (unless it's specifically called out otherwise).

That is, once you've constructed the StreamWriter using fs, you shouldn't perform any direct actions on fs yourself.


The part you quoted from MSDN relates to the later sentences:

This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

That is, you may have passed data to Write such that you've given it some Unicode surrogates, but not a complete character. Flush will not write those surrogates to the stream. So long as you're always passing well formed (complete) strings to Write, you do not need to concern yourself about this.


这篇关于StreamWriter.Flush()还会调用FileStream.Flush()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆