2016-04-09 208 views
0

我使用下面的方法來分析一個文本文件,並編寫包含特定關鍵字行下面一行:搜索關鍵詞,打印線和

using (StreamReader sr = new StreamReader("C:/Users/Downloads/apple.txt")) 
     { 
      string appleLine; 
      bool lastLine = false; 
      // currentLine will be null when the StreamReader reaches the end of file 
      while ((appleLine = sr.ReadLine()) != null) 
      { 
       // Search, case insensitive, if the currentLine contains the searched keyword 
       if (appleLine.IndexOf("Apple", StringComparison.CurrentCultureIgnoreCase) >= 0 || lastLine) 
       { 
        Console.WriteLine(appleLine); 
        Console.WriteLine(); 
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Downloads\parsed.txt", true)) 
        { 
         file.WriteLine(appleLine); 
         file.WriteLine(); 
        } 
        lastLine = true; 
       } 

       if (lastLine) 
       { 
        lastLine = false; 
       } 
      } 

在apple.txt我有事像這樣:

---一號線蘋果MacBook Pro ---

--- 2號線www.newegg.com ---

但這並不打印出與URL行(行2) 。 apple.txt文件可能有200行。

非常感謝您的幫助!

回答

0

嘗試使用布爾值來顯示您發現要打印的內容,並且需要打印下一行。例如:

class Program 
{ 
    static void Main(string[] args) 
    { 
     bool nextLineToPrint = false; 
     using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv")) 
     { 
      string currentLine; 
      // currentLine will be null when the StreamReader reaches the end of file 
      while ((currentLine = sr.ReadLine()) != null) 
      { 
       if (nextLineToPrint) 
       { 
        Console.WriteLine(currentLine); 
        nextLineToPrint = false; 
       } 
       // Search, case insensitive, if the currentLine contains the searched keyword 
       if (currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0) 
       { 
        Console.WriteLine(currentLine); 
        nextLineToPrint = true; 
       } 
      } 
     } 
     Console.ReadLine(); 
    } 
} 
+0

這一個工作;) – tedder84

0

您可以嘗試使用指示'currentline'是否已打印的布爾值,然後在打印下一行後跳出循環。這樣不僅可以打印currentLine,還可以打印下一行,之後將退出while循環。

using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv")) 
{ 
    string currentLine; 
    **bool lastline = false;** 

    // currentLine will be null when the StreamReader reaches the end of file 
    while((currentLine = sr.ReadLine()) != null) 
    { 
     // Search, case insensitive, if the currentLine contains the searched keyword 
     if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0 **|| lastline**) 
     { 
      Console.WriteLine(currentLine); 
      lastline = true; 
     } 

     **//Optional, could also say lastline = false inside if, in case you want to keep looping after printing** 
     **if(lastline){ 
       break; 
     }** 
    } 
} 
+0

這就是我想說的。打印currentLine和lastLine後,它應該循環。無論如何,我沒有得到它的工作。 – tedder84

0

沒有一個更精確的問題陳述,不可能知道你想要的輸出。但是,像下面這樣將滿足作爲迄今爲止約束目標:

using (StreamReader sr = new StreamReader("c:/temp/ESMDLOG.csv")) 
{ 
    bool printWindow = false; 
    Queue<string> window = new Queue<string>(); 
    string currentLine; 
    // currentLine will be null when the StreamReader reaches the end of file 
    while((currentLine = sr.ReadLine()) != null) 
    { 
     bool nextPrintWindow = false 

     // Search, case insensitive, if the currentLine contains the searched keyword 
     if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0) 
     { 
      nextPrintWindow = true; 
     } 

     if (window.Count >= 3) 
     { 
      window.Dequeue(); 
     } 

     window.Enqueue(currentLine); 

     if (printWindow) 
     { 
      Console.WriteLine(string.Join(Environment.NewLine, window)); 
     } 

     printWindow = nextPrintWindow; 
    } 
} 

(警告:。瀏覽器只以上代碼請原諒錯別字和其他疏忽)

以上保持window集合,即文件中的文本的三行「窗口」,通過排隊當前行,並且如果集合達到三個元素的計數則將最早的行出列。

同時,它保留一個標誌,指示是否在讀取下一行後打印窗口的內容。如果此標誌設置爲true,它將window集合中的行連接換行符並將其打印出來。

這樣,如果當前行符合您的匹配條件,則當前行以及前一行和下一行都將被打印出來。

請注意,上述內容將爲每場比賽打印三行窗口。例如,如果連續的行符合匹配標準,則行可以被多次打印(作爲不同三行組的一部分)。如果連續的行符合匹配標準,則替代實現將允許窗口增長超過三個元素,只有在讀取了不符合匹配標準的行時纔打印(並重新啓動)窗口。

在打印窗口之前,即如果要確保即使是不匹配的行只打印過一次,又一替代方案將要求兩個不匹配的行。

我將這些變體留作讀者的練習。它們與上述內容不會有太大的不同,只是對窗口的維護和隱含狀態機的管理在標誌變量中進行了微小的改變。

+0

我試圖做出更精確的問題陳述。 – tedder84

+0

你已經完全改變了這個問題。您不再要求在找到的行之前,之後幷包括該行打印行。如果您確實想要提出其他問題,請將其作爲新問題發佈,而不是更改現有問題。如果你不想問一個新問題,那麼不要改變這個問題。無論哪種方式,請回滾您的編輯,以便您的原始問題仍然存在。請注意,更改問題會使現有答案無效並浪費每個人的時間。 –