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

前端 未结 6 1292
天命终不由人
天命终不由人 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:00

    If the lines are fixed then the most efficient way is as follows:

    using( Stream stream = File.Open(fileName, FileMode.Open) )
    {
        stream.Seek(bytesPerLine * (myLine - 1), SeekOrigin.Begin);
        using( StreamReader reader = new StreamReader(stream) )
        {
            string line = reader.ReadLine();
        }
    }
    

    And if the lines vary in length then you'll have to just read them in a line at a time as follows:

    using (var sr = new StreamReader("file"))
    {
        for (int i = 1; i <= 5; ++i)
            sr.ReadLine();
    }
    

提交回复
热议问题