通过Java将echo的输出重定向到文件

时间:2013-01-28 11:55:02

标签: java process processbuilder io-redirection

  

可能重复:
  ProcessBuilder redirecting output

以下代码:

ProcessBuilder pb = new ProcessBuilder(new String[] {"echo", "some text", ">", "test"});

不断返回“some text > test”。

我做错了什么?

修改

这很有用

ProcessBuilder pb = new ProcessBuilder(new String[] {"bash", "-c", "echo sometext > test"});

1 个答案:

答案 0 :(得分:3)

尝试以下

    ProcessBuilder pb = 
new ProcessBuilder("cmd.exe", "/c" ,"echo", "some text", ">", "test");

这适用于 windows

实际上'Echo'不是一个命令,它是windows中的shell(cmd.exe)的内部命令,而 linux unix 中的“bash”。所以,对于Unix / Linux

    ProcessBuilder pb = 
new ProcessBuilder("bash", "-c","echo", "some text", ">", "test");