写入 Java 进程的 InputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18903549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:10:53  来源:igfitidea点击:

Writing to InputStream of a Java Process

javaprocessstream

提问by Gx.Zero

I have a code that starts a java process (i.e.: executing a compiled java code) via

我有一个启动java进程的代码(即:执行编译的java代码)通过

ProcessBuilder builder = new ProcessBuilder("java", "Sample", "arg1", "arg2");
builder.redirectErrorStream(true);
Process process = builder.start();

Through this, I can basically process the output and errors

通过这个,我基本上可以处理输出和错误

OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

// reader.readLine() blah blah

Now, how can I send input to the stdin? That is, if the code executed by the process has a line that waits for an input as in:

现在,我如何将输入发送到stdin? 也就是说,如果进程执行的代码有一行等待输入,如:

Scanner scan = new Scanner(System.in);
String val = scan.nextLine();
System.out.println(val);

I tried this:

我试过这个:

writer.write("I'm from the stdin!.");
writer.flush();

Though nothing happened. The console still waited for an input.

虽然什么也没发生。控制台仍在等待输入。

Any thoughts?

有什么想法吗?

EDIT:The question was answered, as accepted below. I'm editing to show the faulty code(which I failed to include btw. Lol).

编辑:问题已回答,如下所示。我正在编辑以显示错误的代码(我没有包括 btw。大声笑)。

Before the writer.write()part, I had a

在这writer.write()部分之前,我有一个

String line;
line = reader.readLine();
while (line != null) {
    System.out.println(line);
    line = reader.readLine();
}

采纳答案by Sotirios Delimanolis

The ProcessOutputStream(our point of view) is the STDIN from the process point of view

ProcessOutputStream(我们的观点)是从过程来看的STDIN

OutputStream stdin = process.getOutputStream(); // write to this

So what you have should be correct.

所以你所拥有的应该是正确的。

My driver (apply your own best practices with try-with-resources statements)

我的驱动程序(通过 try-with-resources 语句应用您自己的最佳实践)

public class ProcessWriter {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder("java", "Test");
        builder.directory(new File("C:\Users\sotirios.delimanolis\Downloads"));
        Process process = builder.start();

        OutputStream stdin = process.getOutputStream(); // <- Eh?
        InputStream stdout = process.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

        writer.write("Sup buddy");
        writer.flush();
        writer.close();

        Scanner scanner = new Scanner(stdout);
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    }
}

My application

我的应用程序

public class Test {

    public static void main(String[] args) throws Exception {
        Scanner console = new Scanner(System.in);
        System.out.println("heello World");
        while(console.hasNextLine()) {
            System.out.println(console.nextLine());
        }
    }
}

Running the driverprints

运行驱动程序打印

heello World
Sup buddy

For some reason I need the close(). The flush()alone won't do it.

出于某种原因,我需要close(). 该flush()不会单独做到这一点。

EditIt also works if instead of the close()you provide a \n.

编辑如果close()您提供一个\n.

So with

所以与

writer.write("Sup buddy");
writer.write("\n");
writer.write("this is more\n");
writer.flush();    

the driver prints

驱动程序打印

heello World
Sup buddy
this is more

回答by Tamir Adler

You need to add the end line character ("\n").

您需要添加结束行字符(“\n”)。

like:

喜欢:

writer.write("I'm from the stdin!.\n");
writer.flush();