我尝试通过以下方式与进程进行通信:
Process process = Runtime.getRuntime().exec("/home/username/Desktop/mosesdecoder/bin/moses -f /home/username/Desktop/mosesdecoder/model/moses.ini");
while (true) {
OutputStream stdin = null;
InputStream stderr = null;
InputStream stdout = null;
stdin = process.getOutputStream();
stderr = process.getErrorStream();
stdout = process.getInputStream();
// "write" the parms into stdin
line = "i love you" + "\n";
stdin.write(line.getBytes());
stdin.flush();
stdin.close();
// Print out the output
BufferedReader brCleanUp =
new BufferedReader(new InputStreamReader(stdout));
while ((line = brCleanUp.readLine()) != null) {
System.out.println("[Stdout] " + line);
}
brCleanUp.close();
}这可以很好地工作。然而,当我多次编写管道时,我会遇到一个问题。也就是说,我可以多次写入Outputstream管道。错误是(对于第2次迭代):
Exception in thread "main" java.io.IOException: **Stream Closed**
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:297)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.**flush(BufferedOutputStream.java**:140)
at moses.MOSES.main(MOSES.java:60)那么,有没有办法解决这个问题呢?
发布于 2013-01-07 08:48:25
在while {}循环中,您将调用stdin.close()。第一次通过循环时,从进程中检索到流,并且恰好是打开的。在循环的第一次迭代中,从进程中检索流,将其写入、刷新和关闭(!)。然后循环的后续迭代从进程中获得相同的流,但是在循环的第一次迭代(!)时它被关闭,并且您的程序抛出一个IOException。
https://stackoverflow.com/questions/14188385
复制相似问题