2014-12-03 113 views
0

我在java中編寫了一個小型文本文件解析器,我需要在Visual Basic中重做它,所以簡單的exe可以從PC移動到PC。VB在文本文件中讀取時跳過行

我無法讓VB 2010快速省略帶關鍵字的行。

這裏是工作

public static void main(String[] args) { 
    try { 
     BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("/Users/leighlarue/Desktop/9-18-13.cap"))); 
     StringBuffer stringBuffer = new StringBuffer(); 
     String line; 
     while ((line = bufferedReader.readLine()) != null) { 
      Pattern pattern = Pattern.compile("ALL|MESSAGE|Time|PAPER_MAIN|paper_proc|IOMASTER|Options:|ERROR|Message|BAD GAUGE|Errors|GSP"); 
      if (pattern.matcher(line).find()) { 
       continue; 
      } 
      stringBuffer.append(line); 
      stringBuffer.append("\n"); 
     } 

     BufferedWriter bwr = new BufferedWriter(new FileWriter(new File("/Users/leighlarue/Desktop/Stage_One.txt"))); 

     bwr.write(stringBuffer.toString()); 

     bwr.flush(); 

     bwr.close(); 
     //System.out.println(stringBuffer); 

    } catch (IOException e) { 

    } 
} 

}

有人可以幫我這個轉換爲Visual Basic中,請在Java?

我想做...

Dim strFile As String = TextBox1.Text 

    ' open file into stream reader 
    Dim sr As New StreamReader(strFile) 
    Dim line As String 
    ' get the first line 
    While sr.Peek <> -1 
     line = sr.ReadLine() 
     If line.Contains("MESSAGE") Then 
      Continue While 
     End If 
     RichTextBox1.Text += line + CtrlChars.CrLf 
     ' lineRead = sr.ReadLine() 
    End While 

End Sub 
+1

使用正則表達式,它將允許您定義更符合您在Java中做什麼的模式。 http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx – 2014-12-03 15:53:47

回答

0

您可以使用RegularExpressions更換模式。

' This can be initialized outside your loop 
Dim regex As New System.Text.RegularExpressions.Regex("ALL|MESSAGE|Time|PAPER_MAIN|paper_proc|IOMASTER|Options:|ERROR|Message|BAD GAUGE|Errors|GSP") 

If regex.Matches(line).Count > 0 Then 
    Continue While 
End If 
+0

我改變了代碼,它似乎工作,但一旦數據在richtextbox中,程序被鎖定,並且vshost32.exe沒有在任務管理器下響應。有任何想法嗎? – 2014-12-03 21:44:16

+0

@DanielLaRue真的很難說......你有很多數據嗎? – 2014-12-04 02:36:05

+0

是的,我認爲這是問題所在,當我讓它運行時它完成了。我最終使richtextbox不可見,並在if語句結束時將其設置爲可見,並且它仍需要〜1分鐘才能解析。 – 2014-12-04 03:31:10

0
Dim strFile As String = TextBox1.Text 

    ' open file into stream reader 
    Dim sr As New StreamReader(strFile) 
    Dim line As String 
    ' get the first line 
    While sr.Peek <> -1 
     line = sr.ReadLine() 
     If not line.Contains("MESSAGE") Then 
      RichTextBox1.Text += line + CtrlChars.CrLf 
' Or better yet, use a StringBuilder object. 
     End If 
    End While 

End Sub 
+0

我不明白這是如何回答問題 – 2014-12-04 18:14:54

+0

您可以添加你的答案的更多細節清楚地說明了這個答案和原始問題之間的區別? – Krease 2014-12-04 19:03:32

相關問題