How to get Number Of Lines without Reading File To End

前端 未结 3 1839
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 08:43

Is there a way to get Number of Lines within a Large Text file ,but without Reading the File content or reading file to end and Counting++.

Maybe there are some File Att

3条回答
  •  面向向阳花
    2021-02-07 09:18

    No. You have to read the file. Consider storing it at the beginning of the file or in a separate file when you write the file if you want to find it quickly without counting.

    Note that you can stream the file, and it's surprisingly fast:

    int count = File.ReadLines(path).Count();
    

    Because i might be in some cases where i should get Total Number of Line's and compare it to Current line to display the Percentage,and just for a Percentage Display it might be stupid to read first all Content than read it Again to Display the raw text at user.

    Oh, just get the file size and the length of each line in bytes and keep a cumulative count of the number of bytes processed so far.

提交回复
热议问题