byte[] bytes = value.getBytes();
Process q = new ProcessBuilder("process","arg1", "arg2").start();
q.getOutputStream().write(bytes);
q.getOutputStream().flush();
System.out.println(q.getInputStream().available());
我正在尝试将文件内容流式传输到可执行文件并捕获输出,但输出 (InputStream) 始终为空。如果我指定文件位置但不使用流式输入,我可以捕获输出。
我该如何克服这个问题?
最佳答案
尝试使用 BufferedInputStream()
和 BufferedOutputStream()
包装您的流:
http://download.oracle.com/javase/6/docs/api/java/lang/Process.html#getOutputStream%28%29
Implementation note: It is a good idea for the output stream to be buffered.
Implementation note: It is a good idea for the input stream to be buffered.
即使使用缓冲流,如果您正在处理大量数据,缓冲区仍有可能被填满,您可以通过启动一个单独的线程从 q.getInputStream()< 中读取来处理这个问题
,因此您仍然可以在写入进程的同时从进程中读取。
关于java - 如何将字节数组写入流程构建器的 OutputStream (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5119878/