System.IO.StreamWriter doesn't write for entire for loop

别等时光非礼了梦想. 提交于 2021-02-05 06:27:21

问题


I am trying to write a long list of numbers to a file in C# but it keeps stopping before the end of the list. For example the following code:

  System.IO.StreamWriter file = new System.IO.StreamWriter("D:\\test.txt");

        for (int i = 0; i < 500; i++)
        {
            file.WriteLine(i);
        }

Leaves me with a text file listing the numbers 0 to 431. Changing 500 to 1000 gets me 0 to 840. It just always seems to stop writing before the loop is finished. Outputing the numbers to console as well as file successfully gives the full list in the console, but not the file.


回答1:


You need to close the writer before exiting your program, to make sure that all the buffered output gets written to the file.

A one very convenient way of doing it is with the using statement, which ensures that the StreamWriter gets closed once your loop is done with it:

using (System.IO.StreamWriter file = new System.IO.StreamWriter("D:\\test.txt"))
{
    for (int i = 0; i < 500; i++)
    {
        file.WriteLine(i);
    }
}



回答2:


You discovered that StreamWriter uses a buffer for output, it is 4096 bytes long. This buffer helps making it more efficient, cutting down on the number of calls it makes to the operating system's WriteFile() function. Ensuring that the buffer content makes it to the file requires calling the Flush() method or closing the stream with Close() or Dispose().

If you are sure that you want to keep the file opened then you can add this line of code to ensure that you can see the output when it is written:

    file.AutoFlush = true;

The AutoFlush property is by default false for files. It is true for the Console which is why you can see the output of Console.Write/Line() immediately. Also one of the reasons why console output is so slow.

But given that your file variable is a local variable, you almost surely want to just close the file. Use the using statement to ensure that's taken care of by the time the method that contains this code returns. Forgetting to do this leaves the file opened for a while, at least until the next garbage collection.




回答3:


StreamWriter is buffered, meaning it doesn't write to the file each time you call write.

Since you don't close the StreamWriter yourself you are relying on the finalizer for the object to flush the remaining bytes that are in the buffer.

Change your code to put the StreamWriter inside a using statement and the contents will be flushed as soon as the using block is exited.



来源:https://stackoverflow.com/questions/19208817/system-io-streamwriter-doesnt-write-for-entire-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!