为了账号安全,请及时绑定邮箱和手机立即绑定

使用 StreamReader 跳过行

使用 StreamReader 跳过行

C#
慕哥6287543 2022-01-09 09:57:13
我有一个非常大的文件,大约有 30.000 行。我必须解析这个文件并且不能删除它上面的条目。所以我的想法是跳过已经读过的行。我试过这样的事情:                //Gets the allready readed lines                int readLines = GetCurrentCounter();                //Open File                FileStream stream = new FileStream(LogDatabasePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);                using (StreamReader reader = new StreamReader(stream))                {                    int counter = 0;                    string line;                    //If File was allready read to a specified line, skip these lines                    if (readLines != 0) reader.ReadLine().Skip(readLines);                    //Check if new lines are available                    while ((line = reader.ReadLine()) != null)                    {                        if (counter >= readedLines)                        {                            //If there is text which contains the searched Testsystem-Name                            if (line.Contains(TestSystemName.ToUpper()))                            {                                //Create new Database-Entry                                new TestsystemError().GenerateNewDatabaseEntry(line, counter);                            }                        }                        System.Console.WriteLine(line);                        counter++;                    }                }问题是,功能 reader.ReadLine().Skip(readLines) 没有功能,或者我以错误的方式使用它。我需要在不使用函数“reader.ReadLine()”的情况下跳过行,因为这非常慢(如果我必须遍历所有行 ~ 大约 30.000 行,我会遇到性能问题)。有没有办法跳过行?如果是这样,分享代码会很棒。谢谢。
查看完整描述

3 回答

?
哆啦的时光机

TA贡献1779条经验 获得超6个赞

该方法reader.ReadLine()返回一个字符串。


扩展方法Skip(readedLines)迭代该字符串并返回一个已跳过readedLines字符串中第一个字符的迭代器。


这对读者没有影响。


如果你想跳过第一ñ线,无论是读第一ñ通过调用线reader.ReadLine() ň倍,或读取流,直到你在阅读ñ结束行字符序列建立在读者面前。后一种方法避免为要忽略的行创建字符串,但代码更多。


如果您碰巧有非常规则的数据,因此所有行的长度都相同,那么您可以在创建阅读器之前跳过流


FileStream stream = new FileStream(LogDatabasePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


stream.Seek(readedRows * lengthOfRowInBytes, SeekOrigin.Begin);


using (StreamReader reader = new StreamReader(stream))

  // etc

如果您在行中编码了行号,您也可以进行二进制搜索,但这是更多代码。


查看完整回答
反对 回复 2022-01-09
?
守候你守候我

TA贡献1802条经验 获得超10个赞

与其跟踪行数,不如跟踪读取的字符数。然后您可以使用stream.Seek()快速跳到最后读取的位置,而不是每次都遍历整个文件。


long currentPosition = GetCurrentPosition();


//Open File

FileStream stream = new FileStream(LogDatabasePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using (StreamReader reader = new StreamReader(stream))

{

    string line;


    // Seek to the previously read position

    stream.Seek(currentPosition, SeekOrigin.Begin);


    //Check if new lines are available

    while ((line = reader.ReadLine()) != null)

    {

        // do stuff with the line

        // ...

        Console.WriteLine(line);


        // keep track of the current character position

        currentPosition += line.Length + 2; // Add 2 for newline

    }

}


SaveCurrentPosition(currentPosition);


查看完整回答
反对 回复 2022-01-09
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

您应该在阅读时跳过这些行


//If File was allready read to a specified line, skip these lines

while ((line = reader.ReadLine()) != null && readLines < readedLines){

   readLines++

if (readedLines != 0) reader.ReadLine()

//Check if new lines are available

while ((line = reader.ReadLine()) != null)


查看完整回答
反对 回复 2022-01-09
  • 3 回答
  • 0 关注
  • 386 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号