我試圖在日誌文件上寫出通過Runtime.getRuntime()。exec()啓動的輸出或Java程序。我使用下面的代碼。使用Runtime.getRuntime()啓動的Java程序的輸出exec()
public class Thread_Write extends Thread {
int id;
public void run() {
try {
File log = new File("./log"+id+".txt");
log.createNewFile();
FileWriter fw = new FileWriter("./"+"log"+id+".txt",true);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("java WriteToFile "+id);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
fw.write(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Thread_Write(int i) {
super();
this.id = i;
}
public static void main(String[] args) {
for (int i =0 ; i<10;i++) {
(new Thread_Write(i)).start();
}
}
}
「java WriteToFile id」假設在終端上寫入異常,但不幸的是,我沒有收到任何東西。奇怪的是,如果我改變這個命令,「回聲測試」,「測試」將被正確添加到日誌文件的末尾。任何想法爲什麼?
乾杯。
可能是因爲它寫入了進程的錯誤流,而不是它的輸入流。 – assylias