欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java需要关注的知识点---I0之进程控制

程序员文章站 2022-04-09 13:09:34
...
进程控制:

public class OSExecute {
public static void command(String command) {
boolean err = false;
try {
Process process = new ProcessBuilder(command.split(" ")).start();
BufferedReader result = new BufferedReader(new InputStreamReader(process.getInputStream()));
String s;
while((s = result.readLine())!= null) {
System.out.println(s);
}
BufferedReader errors = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((s = errors.readLine())!= null) {
System.err.println(s);
err = true;
}
} catch (IOException e) {
if(!command.startsWith("CMD /C"))
command("CMD /C" + command);
else
throw new RuntimeException(e);
}
if (err) {
throw new OSExecuteException("Errors executing " + command);
}
}
}
相关标签: IO