简体   繁体   中英

VB skipping lines while reading in text file

i wrote a small text file parser in java and i need to redo it in Visual Basic so the simple exe can be moved from PC to PC.

i am having trouble getting VB 2010 express to omit lines with keywords.

here is the java that worked

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) {

    }
}

}

Can someone help me convert this to Visual Basic please?

what i am trying...

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

You can use RegularExpressions to replace Pattern.

' 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM