请考虑以下代码:
String commandf = "ls /etc | grep release";
try {
// Execute the command and wait for it to complete
Process child = Runtime.getRuntime().exec(commandf);
child.waitFor();
// Print the first 16 bytes of its output
InputStream i = child.getInputStream();
byte[] b = new byte[16];
i.read(b, 0, b.length);
System.out.println(new String(b));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
Run Code Online (Sandbox Code Playgroud)
该程序的输出是:
/etc:
adduser.co
Run Code Online (Sandbox Code Playgroud)
当我从shell运行时,它当然按预期工作:
poundifdef@parker:~/rabbit_test$ ls /etc | grep release
lsb-release
Run Code Online (Sandbox Code Playgroud)
互联网告诉我,由于管道行为不是跨平台的事实,在Java工厂生产Java的杰出人才无法保证管道工作.
我怎样才能做到这一点?
我不会做所有使用Java构建,而不是我的解析grep和sed,因为如果我想改变的语言,我将被迫在语言,这完全是一个没有去重新写我的解析代码.
在调用shell命令时如何让Java进行管道和重定向?