【问题标题】:How to print the command line output in a text area line by line using java?如何使用java逐行打印文本区域中的命令行输出?
【发布时间】:2020-06-24 03:40:14
【问题描述】:

我有一个运行命令的代码,但它的输出没有同时进入文本区域。
它在所有过程完成后显示输出。
所以我需要在文本区域中逐行打印输出。

这是我的代码:

try{
            Process p = Runtime.getRuntime().exec(cmd,null,dir);

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            // read the output from the command

            while ((k = stdInput.readLine()) != null) {
                 //result= result +"\n"+k+"\n";
                 edlflashTextArea.append(k);
                 System.out.println(k);
            }

            // read any errors from the attempted command

            while ((k = stdError.readLine()) != null) {
                //result= result+"\n"+k+"\n";
                edlflashTextArea.append(k);
            }
            edlflashTextArea.append("DONE");


} catch (IOException l) {
            System.out.println("exception happened - here's what I know: ");
            StringWriter sw= new StringWriter();
            l.printStackTrace(new PrintWriter(sw));
            String ExceptionAsString=sw.toString();
            System.out.println(ExceptionAsString);
            edlflashTextArea.append(ExceptionAsString);

}

【问题讨论】:

    标签: java swing cmd runtime jtextarea


    【解决方案1】:

    只需在行尾添加一个新行 ("\n") 字符:

    edlflashTextArea.append("\n");
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 1970-01-01
      • 2012-06-10
      • 2023-01-12
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多