[英]TextWriter Object not writing full document
嗨,我有一个巨大的文档,我必须在.NET中使用C#逐行阅读。 然后执行操作,然后再次写入该行。
我正在用一个较小的文件测试代码,实际文件包含992.482行。 我已经尝试了以下代码进行测试:
while (!scenFile.EndOfStream)
{ writer.WriteLine(scenFile.ReadLine().ToString();
}
而且我只能写992.474。 然后我尝试使用writer.Flush();
System.IO.TextWriter writer = File.CreateText(filepath);
StreamReader scenFile = new StreamReader(filepath2);
while (!scenFile.EndOfStream)
{ (here will go my do-something-function)
{
blah blah
}
writer.WriteLine(scenFile.ReadLine().ToString();
writer.Flush();
}
writer.Close();
然后,我得到了所有的答案。 在代码中插入此行之后,我检查了唯一可以获得的方法是键入“ writer.Flush();”。 在每次迭代中。 我试图将其插入循环中,以便使用“ writer.Flush();”。 每重复一定的次数,我就尝试从50到500.000的数字,但我无法获得所有结果。
问题是我将必须使用实际文件大小的30倍来执行该操作,并且我需要尽快执行该操作。 有谁知道这是为什么,是否有解决方案?
提前致谢
您无需冲洗流两次。 您应该能够在关闭之前冲洗它...
while (!scenFile.EndOfStream)
{ (here will go my do-something-function)
{
blah blah
}
writer.WriteLine(scenFile.ReadLine().ToString();
}
writer.Flush();
writer.Close();
您是在说这不起作用吗?
解决了。
在C#中,Flush()不仅会释放为缓冲区保留的内存。 它还将“将其写入基础流”( StreamWriter.Flush() )。 因此,我所做的只是在$while
之后$writer.Flush()
调用$writer.Flush()
。
这是我最终的解决方案
System.IO.TextWriter writer = File.CreateText(filepath);
StreamReader scenFile = new StreamReader(filepath2);
int count = 0;
while (!scenFile.EndOfStream)
{ (here will go my do-something-function)
{
blah blah
}
writer.WriteLine(scenFile.ReadLine().ToString();
count ++;
if(count == 500000)
{
writer.Flush();
count = 0;
}
}
writer.Flush();
writer.Close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.