How to read a text file reversely with iterator in C#

后端 未结 11 2020
甜味超标
甜味超标 2020-11-22 04:05

I need to process a large file, around 400K lines and 200 M. But sometimes I have to process from bottom up. How can I use iterator (yield return) here? Basically I don\'t l

11条回答
  •  春和景丽
    2020-11-22 04:38

    In case anyone else comes across this, I solved it with the following PowerShell script which can easily be modified into a C# script with a small amount of effort.

    [System.IO.FileStream]$fileStream = [System.IO.File]::Open("C:\Name_of_very_large_file.log", [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
    [System.IO.BufferedStream]$bs = New-Object System.IO.BufferedStream $fileStream;
    [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader $bs;
    
    
    $buff = New-Object char[] 20;
    $seek = $bs.Seek($fileStream.Length - 10000, [System.IO.SeekOrigin]::Begin);
    
    while(($line = $sr.ReadLine()) -ne $null)
    {
         $line;
    }
    

    This basically starts reading from the last 10,000 characters of a file, outputting each line.

提交回复
热议问题