从java进程到JTextArea的输出消息

时间:2016-01-13 05:10:15

标签: java swing process jtextarea

我已尝试过这部分的其他链接,但仍然无法得到我需要的东西。因此,我想在这里寻求小组的帮助。

以下是我的代码:

try {
                String file = new File("iperf3.exe").getCanonicalPath();
                String cmd1[] = {file,"-c","ping.online.net","-P","10","-w","710000"};
                Process p1 = Runtime.getRuntime().exec(cmd1);

                BufferedReader input1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));

                String line1;
                while ((line1 = input1.readLine()) != null) {
                    txtConsole1.setText(line1);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

textArea的输出消息只是执行命令的最后一条消息。我可以知道如何将所有输出消息流式传输到textArea?

谢谢。

1 个答案:

答案 0 :(得分:0)

您通过致电txtConsole1来覆盖txtConsole1.setText(line1);之前的内容。在txtConsole1

中获取变量和最后设置内容的所有输出
String line1;
String content;
while ((line1 = input1.readLine()) != null) {
    content += line1;
}
txtConsole1.setText(content);
相关问题