写入子进程的标准输入

时间:2011-04-14 20:14:52

标签: java

我使用以下代码在Java中生成一个进程:

Process newExec = null;
BufferedReader outStream = null;
BufferedReader inStream = null;
BufferedReader errStream = null;

StringBuffer outputBuffer = new StringBuffer();

String PATH_TO_EXEC = config.getExecPath();

try {
    newExec = Runtime.getRuntime().exec(PATH_TO_EXEC + " " +  args);
}
catch(IOException e){
    outputBuffer.append("Error in running executable.");
    e.printStackTrace();
    return outputBuffer.toString();
}

生成进程后,它需要通过stdin输入一些内容。我如何将字符串串流到这个新生成的程序?

1 个答案:

答案 0 :(得分:2)

Process newExec = null;
BufferedReader outStream = null;
BufferedReader inStream = null;
BufferedReader errStream = null;

StringBuffer outputBuffer = new StringBuffer();

String PATH_TO_EXEC = config.getExecPath();

try {
    newExec = Runtime.getRuntime().exec(PATH_TO_EXEC + " " +  args);
    PrintWriter o = new PrintWriter(newExec.getOutoutStream());
    o.println("input to process");
    o.flush();
}
catch(IOException e){
    outputBuffer.append("Error in running executable.");
    e.printStackTrace();
    return outputBuffer.toString();
}