我正在尝试开发一个类,用于读取外部程序的标准输出(使用Process实例,Runtime.getRuntime().exec(cmdLine, env, dir))。 该程序在过程中需要用户输入,并且只有在输入有效时才会继续进行;这似乎导致我尝试读取其输出时出现了问题:
egm.execute(); // run external the program with specified arguments
BufferedInputStream stdout = new BufferedInputStream(egm.getInputStream());
BufferedInputStream stderr = new BufferedInputStream(egm.getErrorStream());
BufferedOutputStream stdin = new BufferedOutputStream(egm.getOutputStream());
int c; //standard output input stream
int e; //standadr error input stream
while((c=stdout.read()) != -1) //<-- the Java class stops here, waiting for input?
{
egm.processStdOutStream((char)c);
}
while((e=stderr.read()) != -1)
{
egm.processStdErrStream((char)e);
}
//...
我该如何修复程序,以便接受有效输入并继续运行? 希望能帮忙解决这个问题!