redirecting output of a cmd to a file
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:
- capture the output of the
ls
in Java (capturing theProcess
input stream, as it's confusingly named) and create a file yourself - use
"sh -c 'ls whatever > file'"
. The-c
executes everything following it in a shell, including redirection.
精彩评论