2010-12-11 222 views
14

我有一个程序,它读取一个文本文件并将其处理为分段。C#如何使用流读取器读取文本文件时跳过行数?

所以问题是如何更改程序以允许程序在使用流读取器读取文件时跳过读取文件的前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("============================================================"); 
     } 
    } 
} 
+1

所以最新的问题与使用注释掉行? – 2010-12-11 18:54:08

+0

它向专家表明.split方法不起作用。 – JavaNoob 2010-12-11 19:00:38

+0

[C#如何在文本转换为数组之后在文本文件中跳过行的可能的重复?](http://stackoverflow.com/questions/4417916/c-how-to-skip-lines-in-text-file-after -text-coverted-array) – ChrisF 2010-12-11 19:01:23

回答

22

尝试以下方法

// Skip 5 lines 
for(var i = 0; i < 5; i++) { 
    tr.ReadLine(); 
} 

// Read the rest 
string remainingText = tr.ReadToEnd(); 
+0

感谢它很好地工作! – JavaNoob 2010-12-11 18:55:50

8

如果线条被固定,则最有效的方式如下:

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

请注意,与其他解决方案不同,这里不会读取您正在跳过的行,并且应该比其他行更快。 – Mathieson 2013-09-18 16:19:56

0

我想这是简单的:

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

如果你想用它多次在程序中则可能是一个好主意,使从StreamReader的继承与能力的自定义类跳过线。

像这样的事情可以做:

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); 
+0

我宁愿使用扩展方法。 – modiX 2015-10-12 11:39:50

3

我会在列表中再添加两条建议。

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

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<string> ReadLines(Stream stream) 
{ 
    using (var reader = new StreamReader(stream)) 
    { 
     while (!reader.EndOfStream) 
     { 
      yield return reader.ReadLine(); 
     } 
    } 
} 

迭代器块将自动清洗本身了,一旦你用它做。 Here是由Jon Skeet撰写的一篇文章,深入介绍了它的工作原理(向下滚动到“And finally ...”部分)。