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

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);

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)
- 3 回答
- 0 关注
- 386 浏览
添加回答
举报