为什么 StreamWriter 无法访问关闭的流,而 StreamReader 可以?
如果我写下以下语句。
fileStream.Close();
reader.Close();
writer.Close();
reader.Close() 语句执行成功。但我收到错误“无法打开关闭的文件”。在第三条语句 writer.Close()
如果我写
fileStream.Close();
writer.Close();
reader.Close();
第二条语句,即 writer.Close() 本身会引发相同的异常。
有人有想法吗?
If I write following statements.
fileStream.Close();
reader.Close();
writer.Close();
reader.Close() statement executes successfully. But I get error "Can't open closed file." on 3rd statement writer.Close()
If I write
fileStream.Close();
writer.Close();
reader.Close();
2nd statement i.e. writer.Close() itself throws the same exception.
Does anyone have idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是因为 writer 在关闭底层流之前调用了flush()。
I think it's because writer calls flush() before closing the underlying stream.
我将不得不猜测您的代码看起来类似于:
不要关闭 fileStream,流编写器现在“拥有”流。关闭它也会关闭文件流。
I'm going to have to guess that your code looks similar to do:
Don't close fileStream, the stream writer now 'owns' the stream. Closing it closes the file stream as well.