c# how do I count lines in a textfile

前端 未结 7 1290
走了就别回头了
走了就别回头了 2021-02-04 13:39

any problems with doing this?

int  i = new StreamReader(\"file.txt\").ReadToEnd().Split(new char[] {\'\\n\'}).Length
7条回答
  •  一整个雨季
    2021-02-04 14:04

    Well, the problem with doing this is that you allocate a lot of memory when doing this on large files.

    I would rather read the file line by line and manually increment a counter. This may not be a one-liner but it's much more memory-efficient.

    Alternatively, you may load the data in even-sized chunks and count the line breaks in these. This is probably the fastest way.

提交回复
热议问题