简体   繁体   中英

C# StreamWriter Buffer Size Does Not Work

I would like to set a small buffer so the buffer can be written to file more frequently. But it seems this does not work. I wrote the following code and check the text file from time to time and find that text is written to file when i = 840 and the file size is exactly 4K, which is the default buffer size. How come?

        using (StreamWriter sw = new StreamWriter("u:\\log.txt", true, Encoding.UTF8, 1))
        {
            for (int i = 0; i < 300000; i++)
            {
                sw.WriteLine(i);
                Console.Write(i);
                Console.ReadLine();
            }
        }

StreamWriter uses an underlying FileStream, and based on the source code, it looks like the buffer size isn't passed to the file stream.

You can google: .net source code streamwriter

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