简体   繁体   English

TextWriter对象未写入完整文档

[英]TextWriter Object not writing full document

Hi I have a huge document which I have to read line by line using C# in .NET. 嗨,我有一个巨大的文档,我必须在.NET中使用C#逐行阅读。 Then perform a operation, and then write that line again. 然后执行操作,然后再次写入该行。

I am testing the code with a smaller file, the actual file contains 992.482 lines. 我正在用一个较小的文件测试代码,实际文件包含992.482行。 I have tried the following code to test: 我已经尝试了以下代码进行测试:

while (!scenFile.EndOfStream)
{   writer.WriteLine(scenFile.ReadLine().ToString();
}

And I could only write 992.474. 而且我只能写992.474。 Then i tried using writer.Flush(); 然后我尝试使用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();

Then, I got all the lines. 然后,我得到了所有的答案。 After inserting this line in the code, I checked, that the only way in that I could obtain is by typing "writer.Flush();" 在代码中插入此行之后,我检查了唯一可以获得的方法是键入“ writer.Flush();”。 in every iteration. 在每次迭代中。 I have tried to insert it in a loop so that I use "writer.Flush();" 我试图将其插入循环中,以便使用“ writer.Flush();”。 every certain number of iterations, I have tried numbers from 50 to 500.000, and I can't get all the lines. 每重复一定的次数,我就尝试从50到500.000的数字,但我无法获得所有结果。

The problem is that I will have to perform the operation with a file which is 30 times the actual file, and I need to do it as fast as possible. 问题是我将必须使用实际文件大小的30倍来执行该操作,并且我需要尽快执行该操作。 Does anyone knows why is this and if there is any solution? 有谁知道这是为什么,是否有解决方案?

Thanks in advance 提前致谢

You don't need to to flush the stream twice. 您无需冲洗流两次。 You should be able to just flush it before the close... 您应该能够在关闭之前冲洗它...

while (!scenFile.EndOfStream)
{   (here will go my do-something-function)
 {
  blah blah
 }
 writer.WriteLine(scenFile.ReadLine().ToString();
}
writer.Flush();
writer.Close();

Are you saying that somehow this didn't work? 您是在说这不起作用吗?

Solved. 解决了。

In C# Flush() won't only free the memory reserved for the buffer. 在C#中,Flush()不仅会释放为缓冲区保留的内存。 It will also "writes it to the underlying stream" ( StreamWriter.Flush() ). 它还将“将其写入基础流”( StreamWriter.Flush() )。 Therefore what I did was just calling $writer.Flush() right after the $while . 因此,我所做的只是在$while之后$writer.Flush()调用$writer.Flush()

This is my final solution will be 这是我最终的解决方案

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM