【发布时间】:2011-06-12 03:06:26
【问题描述】:
我有一个程序Test.java:
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream(new FileOutputStream("test.txt")));
System.out.println("HelloWorld1");
Runtime.getRuntime().exec("echo HelloWorld2");
}
}
这应该将 HelloWorld1 和 HelloWorld2 打印到文件 text.txt。但是,当我查看文件时,我只看到 HelloWorld1。
HelloWorld2 去哪儿了?是不是烟消云散了?
假设我也想将 HelloWorld2 重定向到 test.txt。我不能只在命令中添加“>>test.txt”,因为我会得到一个文件已经打开的错误。那我该怎么做呢?
【问题讨论】:
-
是否需要使用Runtime?
-
@Navi:还有其他选择吗?!告诉。我想知道!还是您的意思是使用 ProcessBuilder
标签: java redirect runtime exec runtime.exec