简体   繁体   English

当我明确关闭Stream时,关闭StreamReader(或StreamWriter)是否有好处?

[英]Is there a benefit in closing StreamReader (or StreamWriter) when I close Stream explicitly?

I have the following code. 我有以下代码。 In here I am using the StreamReader constructor with leaveOpen: true and in order to do that I need to give the previous parameters which I manage to get their default values . 在这里,我将StreamReader构造函数与leaveOpen: true一起使用,为了做到这一点,我需要提供先前的参数,并设法获得其默认值 This is cumbersome. 这很麻烦。 Since I use stream with using do I gain anything for using the StreamReader with using ? 由于我使用streamusing做我获得了使用任何StreamReaderusing Does answer change if it is a StreamWriter instead? 如果它是StreamWriter答案是否会改变?

using (Strem stream = ...)
{
    ...
    using (StreamReader sr = new StreamReader(stream, Encoding.UTF8, true, 1024, true))
    {
        ...
    }
    ...
}

What if any do I lose if use the following code instead? 如果改用以下代码,我会丢失什么?

using (Strem stream = ...)
{
    ...
    StreamReader sr = new StreamReader(stream);

    ...

    ...
}

You do need to close a StreamWriter (generally via the using block), or else data in its buffer could be lost. 您确实需要关闭StreamWriter (通常通过using块),否则可能会丢失其缓冲区中的数据。

Because both StreamReader and StreamWriter default to closing the stream automatically, if you want to eliminate one using block from your code, it should be the Stream that you remove from using . 因为StreamReaderStreamWriter默认为自动关闭流,所以如果要从代码中消除一个using块,则应该是从using删除的Stream

If you can't do that, for example you've borrowed the Stream from elsewhere that doesn't want you to close it, then you must use the leaveOpen parameter you're already aware of. 如果您无法做到这一点,例如,您已从其他不想关闭它的地方借来了Stream ,则必须使用已经知道的leaveOpen参数。 The reason that you can't just omit the using statement for a StreamReader / StreamWriter in order to leave it open, is that the garbage collector will still trigger some cleanup (although not as much) since the object is unreachable... only this will now occur at an unrelated time, creating an unpredictable bug that's very hard to find. 您不能仅为StreamReader / StreamWriter省略using语句以使其保持打开状态的原因是,垃圾回收器仍会触发一些清理操作(尽管不是那么多),因为该对象不可访问...仅此而已现在将在不相关的时间发生,从而创建了很难发现的不可预测的错误。

It is indeed ugly that you can't specify leaveOpen without explicitly controlling the buffer size, etc. May I suggest a helper method along the lines of StreamReader CreateStreamReaderLeaveOpen(Stream) ? 确实,在不显式控制缓冲区大小的情况下不能指定leaveOpen确实是很丑陋的。我可以沿着StreamReader CreateStreamReaderLeaveOpen(Stream)方式建议一个辅助方法吗?

Because you have leaveOpen set to true in the constructor disposing of a StreamReader does nothing execpt call the the Dispose method of the TextReader class , which itself does nothing at all. 因为在构造函数leaveOpen设置为true ,所以处理StreamReader 不会执行任何execpt调用,而TextReader类的Dispose方法本身不会执行任何操作。

protected override void Dispose(bool disposing)
{
    // Dispose of our resources if this StreamReader is closable.
    // Note that Console.In should be left open.
    try {
        // Note that Stream.Close() can potentially throw here. So we need to 
        // ensure cleaning up internal resources, inside the finally block.  
        if (!LeaveOpen && disposing && (stream != null))
            stream.Close();
    }
    finally {
        if (!LeaveOpen && (stream != null)) {
            stream = null;
            encoding = null;
            decoder = null;
            byteBuffer = null;
            charBuffer = null;
            charPos = 0;
            charLen = 0;
            base.Dispose(disposing);
        }
    }
}

With a StreamWriter it does change a bit because it will not flush it's internal buffers to the underlying stream unless you dispose of the writer . 使用StreamWriter确实会有所改变,因为除非您处置writer,否则它将不会将其内部缓冲区刷新到基础流

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM