C# How to skip number of lines while reading text file using Stream Reader?

前端 未结 6 1320
天命终不由人
天命终不由人 2020-12-05 18:50

I have a program which reads a text file and processes it to be seperated into sections.

So the question is how can the program be changed to allow the program to sk

6条回答
  •  天涯浪人
    2020-12-05 19:02

    The StreamReader with ReadLine or ReadToEnd will actually go and read the bytes into the memory, even if you are not processing these lines, they will be loaded, which will affect the app performance in case of big files (10+ MB).

    If you want to skip a specific number of lines you need to know the position of the file you want to move to, which gives you two options:

    1. If you know the line length you can calculate the position and move there with Stream.Seek. This is the most efficient way to skip stream content without reading it. The issue here is that you can rarely know the line length.
    var linesToSkip = 10;
    using(var reader = new StreamReader(fileName) )
    {
        reader.BaseStream.Seek(lineLength * (linesToSkip - 1), SeekOrigin.Begin);
        var myNextLine = reader.ReadLine();
        // TODO: process the line
    }
    
    1. If you don't know the line length, you have to read line by line and skip them until you get to the line number desired. The issue here is that is the line number is high, you will get a performance hit
    var linesToSkip = 10;
    using (var reader = new StreamReader(fileName))
    {
        for (int i = 1; i <= linesToSkip; ++i)
            reader.ReadLine();
    
        var myNextLine = reader.ReadLine();
        // TODO: process the line
    }
    

    And if you need just skip everything, you should do it without reading all the content into memory:

    using(var reader = new StreamReader(fileName) )
    {
       reader.BaseStream.Seek(0, SeekOrigin.End);
    
       // You can wait here for other processes to write into this file and then the ReadLine will provide you with that content
    
       var myNextLine = reader.ReadLine();
       // TODO: process the line
    }
    

提交回复
热议问题