【问题标题】:VB skipping lines while reading in text fileVB读取文本文件时跳过行
【发布时间】:2014-12-03 15:49:58
【问题描述】:

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

【问题讨论】:

标签: java vb.net text readline skip


【解决方案1】:

您可以使用RegularExpressions 替换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

【讨论】:

  • 我更改了代码,它似乎可以工作,但是一旦数据在 Richtextbox 中,程序就会被锁定,并且 vshost32.exe 在任务管理器下没有响应。有什么想法吗?
  • @DanielLaRue 真的很难说...你有很多数据吗?
  • 是的,我认为这是问题所在,当我让它运行时它确实完成了。我最终使richtextbox不可见,并在if语句的末尾将其设置为可见,并且仍然需要大约1分钟来解析。
  • @DanielLaRue 很长一段时间。我建议您将所有字符串放入 StringBuilder 中,最后仅将结果放入文本框中。
【解决方案2】:
 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

【讨论】:

  • 我看不出这是如何回答问题的
  • 您能否在您的答案中添加更多细节以清楚说明此答案与原始问题之间的差异?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
相关资源
最近更新 更多