从java进程向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中吗?

谢谢。

您通过调用txtConsole1.setText(line1);来覆盖txtConsole1以前的内容。在变量中获取所有输出,并在txtConsole1 中最后设置内容

String line1;
String content;
while ((line1 = input1.readLine()) != null) {
    content += line1;
}
txtConsole1.setText(content);

最新更新