简体   繁体   中英

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

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.

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);
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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