关闭一个文件流没有刷新() [英] Close a filestream without Flush()

查看:121
本文介绍了关闭一个文件流没有刷新()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以关闭一个文件流而不调用的 刷新 (在C#)?我明白, 关闭 和的 的Dispose 调用刷新方法首先

解决方案

MSDN是不是100%清楚,但乔恩斯基特是说同花顺,所以在接近去做/处理。它不会伤害,对吧?



从的 FileStream.Close方法 的:




以前写的任何数据到缓冲器复制到
中的文件流之前,文件被关闭,因此没有必要在打电话之前
冲洗调用关闭。继呼吁关闭,对文件
流的任何操作都可能引发异常。关闭被调用一次后,
做什么,如果再次呼吁




处置不清晰:




这方法处置流,通过写后盾
存储中的任何变更和关闭,以释放资源流。




注:评论家也许是正确的,这不是100%来自同花顺清楚:




覆盖上实现一个缓冲流冲洗。使用此方法可以
的任何信息从底层缓冲到目的地移动,
清除缓冲区,或两者兼而有之。根据对象的状态,则
可能必须修改流中的当前位置(
实施例中,如果基础流支持搜索)。有关更多
信息,请参见CanSeek。



在使用的StreamWriter或类的BinaryWriter,不要冲洗
基Stream对象。相反,使用类的冲洗或Close方法,
这可确保数据刷新到底层流
,然后再写入文件。




测试:



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



我能说什么......所有的文件得到了文本 - 也许这就是只是数据太少?



的Test2



  VAR RND =新的随机(); 
变种大小= 1024 * 1024 * 10;
变种randomBytes =新的字节[大小]
rnd.NextBytes(randomBytes); (:\temp\fileNoCloseNoFlush.bin C,FileMode.CreateNew)VAR fileTest = System.IO.File.Open(@)
{
fileTest.Write(randomBytes使用
,0,randomBytes.Length);
}使用
(VAR fileTest = System.IO.File.Open(@C:\temp\fileCloseNoFlush.bin,FileMode.CreateNew))
{
fileTest.Write(randomBytes,0,randomBytes.Length);
fileTest.Close();
}使用
(VAR fileTest = System.IO.File.Open(@C:\temp\fileFlushNoClose.bin,FileMode.CreateNew))
{
fileTest.Write(randomBytes,0,randomBytes.Length);
fileTest.Flush();
}使用
(VAR fileTest = System.IO.File.Open(@C:\temp\fileCloseAndFlush.bin,FileMode.CreateNew))
{
fileTest.Write(randomBytes,0,randomBytes.Length);
fileTest.Flush();
fileTest.Close();
}

和再次 - 每个文件有它字节......对我来说,它看起来像它做什么我读从MSDN:如果您在调用Flush或关闭之前处置......任何想法不要紧


Can I close a file stream without calling Flush (in C#)? I understood that Close and Dispose calls the Flush method first.

解决方案

MSDN is not 100% clear, but Jon Skeet is saying "Flush", so do it before close/dispose. It won't hurt, right?

From 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 is not as clear:

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

Remark: the commentators might be right, it's not 100% clear from the Flush:

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.

TESTS:

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();
}

What can I say ... all files got the text - maybe this is just too little data?

Test2

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();
}

And again - every file got its bytes ... to me it looks like it's doing what I read from MSDN: it doesn't matter if you call Flush or Close before dispose ... any thoughts on that?

这篇关于关闭一个文件流没有刷新()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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