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

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

    If you want to use it more times in your program then it maybe a good idea to make a custom class inherited from StreamReader with the ability to skip lines.

    Something like this could do:

    class SkippableStreamReader : StreamReader
    {
        public SkippableStreamReader(string path) : base(path) { }
    
        public void SkipLines(int linecount)
        {
            for (int i = 0; i < linecount; i++)
            {
                this.ReadLine();
            }
        }
    }
    

    after this you could use the SkippableStreamReader's function to skip lines. Example:

    SkippableStreamReader exampleReader = new SkippableStreamReader("file_to_read");
    
    //do stuff
    //and when needed
    exampleReader.SkipLines(number_of_lines_to_skip);
    

提交回复
热议问题