如何使管道使用Runtime.exec()?考慮以下代碼: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);}該程序的輸出是:/etc:adduser.co當然,當我從shell運行時,它按預期工作:poundifdef@parker:~/rabbit_test$ ls /etc | grep release
lsb-release互聯網告訴我,由于管道行為不是跨平臺的,在Java工廠生產Java的聰明人不能保證管道工作。我該怎么做?我不打算使用Java構造來進行所有的解析,而不是grep和sed,因為如果我想改變語言,我將被迫用該語言重寫我的解析代碼,這完全是不可能的。如何讓Java在調用shell命令時進行管道和重定向?
添加回答
舉報
0/150
提交
取消