c#每200个循环后保存Streamwriter而不关闭
我正在使用StreamWriter将一些数据写入文件c#每200个循环后保存Streamwriter而不关闭,c#,file,save,C#,File,Save,我正在使用StreamWriter将一些数据写入文件 System.IO.StreamWriter file = new System.IO.StreamWriter(path); while(something_is_happening && my_flag_is_true) file.WriteLine(some_text_goes_Inside); file.close(); 我注意到的是,在结束之前,没有数据写入文件 是否有任何方法可以在关闭前将内容保存到文
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
while(something_is_happening && my_flag_is_true)
file.WriteLine(some_text_goes_Inside);
file.close();
我注意到的是,在结束之前,没有数据写入文件
是否有任何方法可以在关闭前将内容保存到文件中。调用强制写入缓冲区:
file.Flush();
清除当前写入程序的所有缓冲区,并产生任何缓冲数据
要写入基础流
或集合属性
获取或设置一个值,该值指示StreamWriter是否将刷新
它的缓冲区在每次调用
StreamWriter.Write
我想你在找我 应该做到这一点。循环:
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
for (int i = 0; true && flag; i++)
{
file.WriteLine(some_text_goes_Inside);
if (i == 200)
{
file.Flush();
i = 0;
}
}
file.Close();
为此,可以使用Flush方法
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
int counter = 0;
while(something_is_happening && my_flag_is_true)
{
file.WriteLine(some_text_goes_Inside);
counter++;
if(counter < 200) continue;
file.Flush();
counter = 0;
}
file.Close();
System.IO.StreamWriter文件=新的System.IO.StreamWriter(路径);
int计数器=0;
当(某事正在发生&我的标志是真的)
{
WriteLine(一些文本放在里面);
计数器++;
如果(计数器<200)继续;
Flush()文件;
计数器=0;
}
file.Close();
有关更多信息,欢迎访问只是一个提示:您应该使用streamwriter的
using
语句。
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
for (int i = 0; true && flag; i++)
{
file.WriteLine(some_text_goes_Inside);
if (i == 200)
{
file.Flush();
i = 0;
}
}
file.Close();
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
int counter = 0;
while(something_is_happening && my_flag_is_true)
{
file.WriteLine(some_text_goes_Inside);
counter++;
if(counter < 200) continue;
file.Flush();
counter = 0;
}
file.Close();