2010-12-11 220 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 ...」部分)。