小编Bra*_*nao的帖子

有没有更好的方法来确定大型txt文件(1-2 GB)中的行数?

我试图计算txt文件中的所有行,我使用StreamReader:

public int countLines(string path)
{
    var watch = System.Diagnostics.Stopwatch.StartNew();
    int nlines=0;
    string line;
    StreamReader file = new StreamReader(path);
    while ((line = file.ReadLine()) != null)
    {
        nlines++;
    }
    watch.Stop();
    var elapsedMs = watch.ElapsedMilliseconds;
    Console.Write(elapsedMs)
    // elapsedMs = 3520  --- Tested with a 1.2 Mill txt
    return nlines;
}
Run Code Online (Sandbox Code Playgroud)

有没有更有效的方法来计算行数?

.net c#

9
推荐指数
2
解决办法
2643
查看次数

标签 统计

.net ×1

c# ×1