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