Is StreamReader.Readline() really the fastest method to count lines in a file?

前端 未结 7 1232
逝去的感伤
逝去的感伤 2020-12-30 06:23

While looking around for a while I found quite a few discussions on how to figure out the number of lines in a file.

For example these three:
c# how do I count l

相关标签:
7条回答
  • 2020-12-30 07:20

    There are numerous ways to read a file. Usually, the fastest way is the simplest:

    using (StreamReader sr = File.OpenText(fileName))
    {
            string s = String.Empty;
            while ((s = sr.ReadLine()) != null)
            {
                   //do what you gotta do here
            }
    }
    

    This page does a great performance comparison between several different techniques including using BufferedReaders, reading into StringBuilder objects, and into an entire array.

    0 讨论(0)
提交回复
热议问题