开发者

redirecting output of a cmd to a file

开发者 https://www.devze.com 2022-12-23 23:05 出处:网络
we are开发者_开发问答 trying to re-direct the output of the list to a file using the below cmd(java) in ubuntu,

we are开发者_开发问答 trying to re-direct the output of the list to a file using the below cmd(java) in ubuntu, Pls let us know if this works or not ?

Process p = Runtime.getRuntime().exec("ls -l >/home/blah blah/new.txt")


No it won't. The '>' is part of the shell and as such executing ls on its own won't help.

You can:

  1. capture the output of the ls in Java (capturing the Process input stream, as it's confusingly named) and create a file yourself
  2. use "sh -c 'ls whatever > file'". The -c executes everything following it in a shell, including redirection.
0

精彩评论

暂无评论...
验证码 换一张
取 消