0

我正在使用命令行参数执行一个可执行文件,ProcessBuilder并且我正在尝试使用BufferdReader. 但是,当我打印出进程的输入流时,似乎我首先打印出输出,然后是输入。

例如,我正在尝试执行"my_command -an-option /path/to/file",当我打印出缓冲的阅读器时,我正在打印输出,然后是我的文件的内容/path/to/file。我想输入流在我的 inputp 和输出中读取是有道理的,

public static void d(String file) throws Exception {
    ProcessBuilder builder = new ProcessBuilder("my_command", "-an-option", file);
    builder.redirectErrorStream(true);

    Process process = builder.start();
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    process.waitFor();

    String s = null;
    while ((s = in.readLine()) != null) System.out.println(s);

    in.close();
}

public static void main(String[] args) {
    d("/path/to/file");
}

有谁知道如何让它只打印输出?我想将输出保存到字符串或其他东西并解析它等。

4

0 回答 0