2

从大日志文件(大约 2532910 行)中,我正在寻找的行很少(比如 10 或 12)。匹配和阅读这些行的最佳方法是什么?我的代码在 c# 中。有没有办法让阅读器/流只能读取模式匹配数据?

谢谢

4

1 回答 1

3

要读取这么大的文件,最好的方法是使用 streamReader.ReadLine()

像这样:

StreamReader sr = new StreamReader(@"path_to_log");

int lineNum = 1;
const int searchingLineNum = 10;
string line = string.Empty;

while (sr.Peek() != -1)
{
    line = sr.ReadLine();

    if (lineNum == searchingLineNum)
    {
        break;
    }
    lineNum++;
}

Console.WriteLine(line); // do what you want with this line (parse using Regex)
于 2010-09-15T19:14:51.187 回答