如果必须使用
Process,则类似这样的方法应该起作用:
public static void pipeStream(InputStream input, OutputStream output) throws IOException{ byte buffer[] = new byte[1024]; int numRead = 0; do { numRead = input.read(buffer); output.write(buffer, 0, numRead); } while (input.available() > 0); output.flush();}public static void main(String[] argv){ FileInputStream fileIn = null; FileOutputStream fileOut = null; OutputStream procIn = null; InputStream procOut = null; try { fileIn = new FileInputStream("test.txt"); fileOut = new FileOutputStream("testOut.txt"); Process process = Runtime.getRuntime().exec ("/bin/cat"); procIn = process.getOutputStream(); procOut = process.getInputStream(); pipeStream(fileIn, procIn); pipeStream(procOut, fileOut); } catch (IOException ioe) { System.out.println(ioe); }}注意:
- 确保
close
流 - 更改为使用缓冲流,我认为原始
Input/OutputStreams
实现可能一次复制一个字节。 - 该过程的处理方式可能会根据您的特定过程而有所不同:这
cat
是使用管道I / O的最简单示例。



