StreamWriter.Flush()和StreamWriter.Close()之间的区别是什么?(What is the difference between StreamWriter.Flush() and StreamWriter.Close())

   IT问题网   2018-12-04 00:00:00

问 题

与 streamwriter.flush()在功能上有什么区别和 streamwriter.close()?

在我的数据没有被正确地写入文件,我说无论同花顺()和关闭()来我的code结束。但是,我意识到,加入的或者的同花顺()或关闭()允许的数据要正确写入。

我无法拿起正是这些方法的确,当我读msdn文档;我只是想通了,一方或另一方是必要的,以保证数据的正确写入。任何进一步的解释会更ap preciated。


其中,取值将被写入字符串,这里就是我的code看起来像当前:

streamwriter的sw = file.createtext("textoutput.txt");
sw.write(多个);
sw.flush();
sw.close();
 

基于来自答案反馈

,我已经重写我的code在使用块,它实现的idisposable 并将自动采取写入流以当对象被布置在文件的护理:

 使用(streamwriter的sw = file.createtext("textoutput.txt"))
{
sw.write(多个);
}
 
解决方案

streamwriter.flush()可以叫你需要清除缓冲区内的任何时间,流将继续开放。

streamwriter.close()是关闭流,此时缓冲区也刷新。

但是,你真的不应该需要调用这些。任何时候,我看到一个 .close()在code我把它看作一个code嗅觉,因为这通常意味着一个意外的异常可能会导致资源悬空。你的应该采取什么的做的,是用块创建的streamwriter 变量,像这样的:

 使用(var作家=新的streamwriter("somefilepath.txt"))
{
//写一堆东西在这里
} //该streamwriter的将被关闭,在这里刷新,即使抛出异常。
 

标签:以及之间区别区分是什么



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

现净switch语句散列或索引?(Are .Net switch statements hashed or indexed)

problem does .net 4 (or any prior version) perform any so ...

使用LINQ生成没有选择直接更新(Use linq to generate direct update without select)

problem g'day everyone. i'm still learning l ...

是否有可能拦截控制台输出?(Is it possible to intercept Console output)

problem i call a method, say, fizzbuzz(), over which i ha ...

当使用排序列表 TKEY的,TValue在一个SortedDictionary TKEY的,TValue?(When to use a SortedList<TKey, TValue> over a SortedDictionary<TKey, TValue>)

problem this may appear to be a duplicate of this questio ...

如何初始化列表 T给定的尺寸(相对于容量)?(How to initialize a List<T> to a given size (as opposed to capacity))

problem .net offers a generic list container whose perfor ...

如何使用XPath用的XDocument?(how to use XPath with XDocument)

problem there is a similar question, but it seems that th ...

什么是WebConfigurationManager和ConfigurationManager中的区别?(What's the difference between the WebConfigurationManager and the ConfigurationManager)

problem what's the difference between the webconfiguratio ...

NUnit的测试运行秩序(NUnit Test Run Order)

problem by default nunit tests run alphabetic ...

如何恢复从C#数据库(How to restore a database from C#)

problem i have a sql 2008 db. i am running a form that ba ...

调用堆栈不说"你来自哪里"但是"你要去哪里,下一个"?(The call stack does not say "where you came from", but "where you are going next")

problem in a previous question (get object ca ...

如何读取和写入ID3标签在C#中的MP3?(How to read and write ID3 tags to an MP3 in C#)

problem is there a library for reading and writing id3 ta ...

如何设置读取权限的X.509证书从.NET私钥文件(How to set read permission on the private key file of X.509 certificate from .NET)

problem here is the code to add a pfx to the cert store. ...

获取列表视图的项目双击事件(Get the item doubleclick event of listview)

problem how to get the item double click event of listvie ...

MongoDB的交易?(MongoDB transactions)

problem playing around with mongodb and norm in .net. th ...

确定是否.NET程序集是从相同的源代码构建(Determine whether .NET assemblies were built from the same source)

problem does anyone know of a way to compare two .net ass ...

如何prevent更新一个ListViewItem的的文字时,在闪烁的ListView?(How to prevent flickering in ListView when updating a single ListViewItem's text)

problem all i want is to update an listviewit ...

谁应该调用Dispose IDisposable的对象时传递到另一个对象?(Who should call Dispose on IDisposable objects when passed into another object)

problem is there any guidance or best practic ...

获得一天的后缀使用则DateTime.ToString时,()(Getting day suffix when using DateTime.ToString())

problem is it possible to include the day suffix when for ...

改变目标框架我在Visual Studio解决方案的所有项目(Change the Target Framework for all my projects in a Visual Studio Solution)

problem i need to change the target framework ...

string.IsNullOrEmpty(串)与string.IsNullOrWhiteSpace(串)(string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string))

problem is use of string.isnullorempty(string ...