[C#]统计文本文件txt中的行数(快速读取)


快速统计文本文件中的行数( StreamReader.ReadLine() ):

测试代码如下:

 1        //读取txt文件中总行数的方法
 2         public static int requestMethod(String _fileName)
 3         {
 4             Stopwatch sw = new Stopwatch();
 5             var path = _fileName;
 6             int lines = 0;
 7 
 8             //按行读取
 9             sw.Restart();
10             using (var sr = new StreamReader(path))
11             {
12                 var ls = "";
13                 while ((ls = sr.ReadLine()) != null)
14                 {
15                     lines++;
16                 }
17             }
18             sw.Stop();
19             return lines;
20         }

 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM