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 關注
- 375 瀏覽
添加回答
舉報