3
如何編寫將運行另一個java程序(應該調用該程序)的程序從該程序給出該程序的輸入並獲取輸出並打印輸出到文件。從源程序給出並獲取java程序的I/O
如何編寫將運行另一個java程序(應該調用該程序)的程序從該程序給出該程序的輸入並獲取輸出並打印輸出到文件。從源程序給出並獲取java程序的I/O
例子:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
// Write to input of the program using outputstream here
...
// Read output of program from input stream here
// ...
FileOutputStream fileOut = new FileOutputStream("output.txt");
BufferedInputStream bIn = new BufferedInputStream(in);
byte buf = new byte[4096];
int count;
while ((count = bIn.read(buf)) != -1) {
fileOut.write(buf, 0, count);
}
...
fileOut.close();
bIn.close();
// Exception handling is left as an exercise for the reader :-P
我沒有聽到的ProcessBuilder類的了。謝謝你的提示! – 2009-02-02 13:51:12