23

我有一个程序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。

  1. HelloWorld2去哪儿了?是不是就凭空消失了?

  2. 假设我也想将 HelloWorld2 重定向到 test.txt。我不能只在命令中添加“>>test.txt”,因为我会得到一个文件已经打开的错误。那么我该怎么做呢?

4

4 回答 4

40

Runtime.exec 的标准输出不会自动发送到调用者的标准输出。

像这样的事情要做 - 访问分叉进程的标准输出,读取它然后写出来。getInputStream()请注意,使用Process 实例的方法,父进程可以使用分叉进程的输出。

public static void main(String[] args) throws Exception {
    System.setOut(new PrintStream(new FileOutputStream("test.txt")));
    System.out.println("HelloWorld1");

     try {
       String line;
       Process p = Runtime.getRuntime().exec( "echo HelloWorld2" );

       BufferedReader in = new BufferedReader(
               new InputStreamReader(p.getInputStream()) );
       while ((line = in.readLine()) != null) {
         System.out.println(line);
       }
       in.close();
     }
     catch (Exception e) {
       // ...
     }
}
于 2011-01-19T23:26:29.273 回答
5

从 JDK 1.5 开始,java.lang.ProcessBuilder 也可以处理 std 和 err 流。它是 java.lang.Runtime 的替代品,您应该使用它。

于 2011-01-19T23:28:01.707 回答
2

System.out 不是您通过调用 exec() 生成的新进程的标准输出。如果您想查看“HelloWorld2”,您必须获取从 exec() 调用返回的 Process,然后从中调用 getOutputStream()。

于 2011-01-19T23:26:59.523 回答
0

实现目标的更简单方法:

    ProcessBuilder builder = new ProcessBuilder("hostname");
    Process process = builder.start();
    Scanner in = new Scanner(process.getInputStream());
    System.out.println(in.nextLine()); // or use iterator for multilined output
于 2019-06-18T12:55:27.217 回答