0
我需要在命令行中執行命令並將其打印在控制檯上。我通過process Builder完成了它。 它正在打印批量輸出(即)它等待整個命令完成執行並最終打印出來,但我想立即打印。每行打印命令行時必須同時打印在控制檯中。當我嘗試調試我發現它是逐行打印。從命令行在控制檯中逐行打印
final BufferedReader m_stdOutput;
final BufferedReader m_stdError;
probuilder.directory(new File("C:\\sss"));
Process m_process = null;
try {
m_process = probuilder.start();
} catch (IOException e1) {
e1.printStackTrace();
}
m_stdOutput = new BufferedReader(new InputStreamReader(m_process.getInputStream()));
m_stdError = new BufferedReader(new InputStreamReader(m_process.getErrorStream()));
Thread m_outputThread = new Thread() {
public void run() {
String line;
try {
while ((line = m_stdOutput.readLine()) != null) {
System.out.println(line);
}} catch (IOException e) {
e.printStackTrace();
}
};
};
m_outputThread.start();
Thread m_errorThread = new Thread() {
public void run() {
String line1;
try {
while ((line1 =m_stdError.readLine()) != null) {
System.out.println(line1);
}} catch (IOException e) {
e.printStackTrace();
}
};
};
m_errorThread.start();
儘量不要使用BufferedReader並從InputStream本身打印每個字符...... – MadProgrammer