我有一个运行命令的代码,但它的输出没有同时进入文本区域。
它在所有过程完成后显示输出。
所以我需要在文本区域中逐行打印输出。
这是我的代码:
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);
}
最佳答案
只需在行尾添加一个新行("\n"
)字符即可:
edlflashTextArea.append("\n");
关于java - 如何使用java在文本区域中逐行打印命令行输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60648158/