c# - 关闭没有 Flush() 的文件流

标签 c# filestream flush

我可以在不调用 Flush 的情况下关闭文件流吗? (在 C# 中)?我明白 CloseDispose首先调用 Flush 方法。

最佳答案

MSDN 不是 100% 清楚,但 Jon Skeet 说“冲洗”,所以在关闭/处置之前进行。不会痛的,对吧?

来自 FileStream.Close Method :

Any data previously written to the buffer is copied to the file before the file stream is closed, so it is not necessary to call Flush before invoking Close. Following a call to Close, any operations on the file stream might raise exceptions. After Close has been called once, it does nothing if called again.

Dispose 不是很清楚:

This method disposes the stream, by writing any changes to the backing store and closing the stream to release resources.

备注:评论员可能是对的,从同花顺中不能100%清楚:

Override Flush on streams that implement a buffer. Use this method to move any information from an underlying buffer to its destination, clear the buffer, or both. Depending upon the state of the object, you might have to modify the current position within the stream (for example, if the underlying stream supports seeking). For additional information see CanSeek.

When using the StreamWriter or BinaryWriter class, do not flush the base Stream object. Instead, use the class's Flush or Close method, which makes sure that the data is flushed to the underlying stream first and then written to the file.

测试:

var textBytes = Encoding.ASCII.GetBytes("Test123");
using (var fileTest = System.IO.File.Open(@"c:\temp\fileNoCloseNoFlush.txt", FileMode.CreateNew))
{
    fileTest.Write(textBytes,0,textBytes.Length);
}
using (var fileTest = System.IO.File.Open(@"c:\temp\fileCloseNoFlush.txt", FileMode.CreateNew))
{
    fileTest.Write(textBytes, 0, textBytes.Length);
    fileTest.Close();
}
using (var fileTest = System.IO.File.Open(@"c:\temp\fileFlushNoClose.txt", FileMode.CreateNew))
{
    fileTest.Write(textBytes, 0, textBytes.Length);
    fileTest.Flush();
}
using (var fileTest = System.IO.File.Open(@"c:\temp\fileCloseAndFlush.txt", FileMode.CreateNew))
{
    fileTest.Write(textBytes, 0, textBytes.Length);
    fileTest.Flush();
    fileTest.Close();
}

我能说什么......所有文件都有文本 - 也许这只是数据太少?

测试2

var rnd = new Random();
var size = 1024*1024*10;
var randomBytes = new byte[size];
rnd.NextBytes(randomBytes);
using (var fileTest = System.IO.File.Open(@"c:\temp\fileNoCloseNoFlush.bin", FileMode.CreateNew))
{
    fileTest.Write(randomBytes, 0, randomBytes.Length);
}
using (var fileTest = System.IO.File.Open(@"c:\temp\fileCloseNoFlush.bin", FileMode.CreateNew))
{
    fileTest.Write(randomBytes, 0, randomBytes.Length);
    fileTest.Close();
}
using (var fileTest = System.IO.File.Open(@"c:\temp\fileFlushNoClose.bin", FileMode.CreateNew))
{
    fileTest.Write(randomBytes, 0, randomBytes.Length);
    fileTest.Flush();
}
using (var fileTest = System.IO.File.Open(@"c:\temp\fileCloseAndFlush.bin", FileMode.CreateNew))
{
    fileTest.Write(randomBytes, 0, randomBytes.Length);
    fileTest.Flush();
    fileTest.Close();
}

再一次 - 每个文件都有它的字节......对我来说,它看起来就像我从 MSDN 上读到的那样:在处理之前调用 Flush 或 Close 并不重要......对此有什么想法吗?

关于c# - 关闭没有 Flush() 的文件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7376956/

相关文章:

c# - WPF UI 更新线程问题

c# - 具有可选身份验证/授权的 ASP.NET Core

C++ 流对象线程安全吗?

java - 关于在调用查询之前使用 JPA 刷新的问题

c - 否定 !my_function() 如何影响我在 C 中的函数?

c# - 字符串插值 : a bug in c# compiler?

c# - 如何检查域中是否存在 Windows 用户帐户名?

c# - 将文件重定向到流 C#

javascript - 运行文件 ://from http://localhost/

PHP 缓冲区 ob_flush() 与 flush()