猿问

使用 StreamReader 跳过行

我有一个非常大的文件,大约有 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 行,我会遇到性能问题)。


有没有办法跳过行?如果是这样,分享代码会很棒。谢谢。


慕哥6287543
浏览 387回答 3
3回答

哆啦的时光机

该方法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如果您在行中编码了行号,您也可以进行二进制搜索,但这是更多代码。

守候你守候我

与其跟踪行数,不如跟踪读取的字符数。然后您可以使用stream.Seek()快速跳到最后读取的位置,而不是每次都遍历整个文件。long currentPosition = GetCurrentPosition();//Open FileFileStream 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);

回首忆惘然

您应该在阅读时跳过这些行//If File was allready read to a specified line, skip these lineswhile ((line = reader.ReadLine()) != null && readLines < readedLines){&nbsp; &nbsp;readLines++}&nbsp;if (readedLines != 0) reader.ReadLine()//Check if new lines are availablewhile ((line = reader.ReadLine()) != null)
随时随地看视频慕课网APP
我要回答