3 回答

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
該方法reader.ReadLine()返回一個(gè)字符串。
擴(kuò)展方法Skip(readedLines)迭代該字符串并返回一個(gè)已跳過readedLines字符串中第一個(gè)字符的迭代器。
這對(duì)讀者沒有影響。
如果你想跳過第一?線,無論是讀第一?通過調(diào)用線reader.ReadLine() ň倍,或讀取流,直到你在閱讀?結(jié)束行字符序列建立在讀者面前。后一種方法避免為要忽略的行創(chuàng)建字符串,但代碼更多。
如果您碰巧有非常規(guī)則的數(shù)據(jù),因此所有行的長度都相同,那么您可以在創(chuàng)建閱讀器之前跳過流
FileStream stream = new FileStream(LogDatabasePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
stream.Seek(readedRows * lengthOfRowInBytes, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
// etc
如果您在行中編碼了行號(hào),您也可以進(jìn)行二進(jìn)制搜索,但這是更多代碼。

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
與其跟蹤行數(shù),不如跟蹤讀取的字符數(shù)。然后您可以使用stream.Seek()快速跳到最后讀取的位置,而不是每次都遍歷整個(gè)文件。
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貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
您應(yīng)該在閱讀時(shí)跳過這些行
//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 關(guān)注
- 369 瀏覽
添加回答
舉報(bào)