VB在文本文件中读取时跳过行

时间:2014-12-03 15:49:59

标签: java vb.net text readline skip

我在java中编写了一个小文本文件解析器,我需要在Visual Basic中重做它,这样简单的exe就可以从PC移到PC上。

我无法让VB 2010 express忽略带有关键字的行。

这是有效的java

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吗?

我正在尝试...

 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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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