C#如何使用Stream Reader读取文本文件时跳过行数?

我有一个程序,它读取文本文件并处理它以分成几个部分。

所以问题是如何更改程序以允许程序在使用Stream Reader读取文件时跳过读取文件的前5行?

有人可以告知代码吗? 谢谢!

代码:

class Program { static void Main(string[] args) { TextReader tr = new StreamReader(@"C:\Test\new.txt"); String SplitBy = "----------------------------------------"; // Skip first 5 lines of the text file? String fullLog = tr.ReadToEnd(); String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None); //String[] lines = sections.Skip(5).ToArray(); foreach (String r in sections) { Console.WriteLine(r); Console.WriteLine("============================================================"); } } } 

请尝试以下方法

 // Skip 5 lines for(var i = 0; i < 5; i++) { tr.ReadLine(); } // Read the rest string remainingText = tr.ReadToEnd(); 

如果线路是固定的,那么最有效的方法如下:

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

如果线条的长度不同,那么你必须一次只读一行,如下所示:

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

如果你想在程序中多次使用它,那么从StreamReaderinheritance自定义类并跳过行是一个好主意。

像这样的东西可以做:

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

在此之后,您可以使用SkippableStreamReader的函数来跳过行。 例:

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

我将在列表中添加两个建议。

如果总有一个文件,你只会阅读,我建议:

 var lines = File.ReadLines(@"C:\Test\new.txt").Skip(5).ToArray(); 

File.ReadLines不会阻止其他文件,只会加载到内存中必需的行。

如果你的流可以来自其他来源,那么我建议这种方法:

 class Program { static void Main(string[] args) { //it's up to you to get your stream var stream = GetStream(); //Here is where you'll read your lines. //Any Linq statement can be used here. var lines = ReadLines(stream).Skip(5).ToArray(); //Go on and do whatever you want to do with your lines... } } public IEnumerable ReadLines(Stream stream) { using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream) { yield return reader.ReadLine(); } } } 

一旦完成,Iterator块将自动清理。 这是Jon Skeet的一篇文章,深入探讨了它是如何工作的(向下滚动到“最后……”部分)。

我猜它就像这样简单:

  static void Main(string[] args) { var tr = new StreamReader(@"C:\new.txt"); var SplitBy = "----------------------------------------"; // Skip first 5 lines of the text file? foreach (var i in Enumerable.Range(1, 5)) tr.ReadLine(); var fullLog = tr.ReadToEnd(); String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None); //String[] lines = sections.Skip(5).ToArray(); foreach (String r in sections) { Console.WriteLine(r); Console.WriteLine("============================================================"); } }