Runtime.exec 执行管道重定向与大文件合并命令

 要执行包含管道重定向与大文件合并的外部命令,需要注意以下两点:

 

1. 需要使用Runtime.exec的以下重载方法:

public Process exec(String[] cmdarray) throws IOException

 

2.需要使用

/bin/sh -c

 

 

例子:

 

使用管道:

String[] cmd = {"/bin/sh", "-c", "netstat -anltp|grep 80|grep ESTA"};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();

使用重定向合并大文件:

String[] cmd = {"/bin/sh", "-c", "cat trace* > tmp.txt"};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();

 我测试过合并生成了一个2G的文件

 

你可能感兴趣的:(Runtime.exec)