第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 StreamReader 跳過行

使用 StreamReader 跳過行

C#
慕哥6287543 2022-01-09 09:57:13
我有一個(gè)非常大的文件,大約有 30.000 行。我必須解析這個(gè)文件并且不能刪除它上面的條目。所以我的想法是跳過已經(jīng)讀過的行。我試過這樣的事情:                //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) 沒有功能,或者我以錯(cuò)誤的方式使用它。我需要在不使用函數(shù)“reader.ReadLine()”的情況下跳過行,因?yàn)檫@非常慢(如果我必須遍歷所有行 ~ 大約 30.000 行,我會(huì)遇到性能問題)。有沒有辦法跳過行?如果是這樣,分享代碼會(huì)很棒。謝謝。
查看完整描述

3 回答

?
哆啦的時(shí)光機(jī)

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)制搜索,但這是更多代碼。


查看完整回答
反對(duì) 回復(fù) 2022-01-09
?
守候你守候我

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


查看完整回答
反對(duì) 回復(fù) 2022-01-09
?
回首憶惘然

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)


查看完整回答
反對(duì) 回復(fù) 2022-01-09
  • 3 回答
  • 0 關(guān)注
  • 369 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)