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

前端 未结 7 1248
逝去的感伤
逝去的感伤 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:13

    public static int CountLines(Stream stm)
    {
        StreamReader _reader = new StreamReader(stm);
        int c = 0, count = 0;
        while ((c = _reader.Read()) != -1)
        {
            if (c == '\n')
            {
                count++;
            }
        }
        return count;
    }
    

提交回复
热议问题