Java 进程 getInputStream 与 getOutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4228853/
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-10-30 05:24:48  来源:igfitidea点击:

Java Process getInputStream vs. getOutputStream

javaprocessinputstream

提问by Tony R

I'm a bit confused about the streams... which is which?

我对流有点困惑......哪个是哪个?

Simply, which stream should I use to catch the output of my Process, and which stream should I use to give my Process some input?

简单地说,我应该使用哪个流来捕获我的 Process 的输出,以及我应该使用哪个流为我的 Process 提供一些输入?

回答by erickson

You can only read from an InputStream, so use that to catch the output of your process.

您只能从 读取InputStream,因此使用它来捕获过程的输出。

You write to an OutputStream, so use that to give the process your input.

你写一个OutputStream,所以用它来给过程你的输入。

You are using names that make sense in the context of the spawned process. But the API names make sense in the context of the parent process.

您正在使用在衍生进程的上下文中有意义的名称。但是 API 名称在父进程的上下文中是有意义的。

Here's another tip: if your process writes to standard error, be sure to read that too. If standard output or error pipes of the sub-process are full (because your parent Java process isn't consuming them), the child process will block on its write()calls.

这是另一个提示:如果您的进程写入标准错误,请务必阅读它。如果子进程的标准输出或错误管道已满(因为您的父 Java 进程没有使用它们),则子进程将阻塞其write()调用。

回答by Jon Skeet

I always ignore the names and look at what's returned. If your code has an OutputStream, you can writeto it - which means it's the inputfor the other process. If your code has an InputStream, you can readfrom it - which means it's the outputor error for the other process.

我总是忽略名称并查看返回的内容。如果您的代码有一个OutputStream,您可以写入它 - 这意味着它是另一个进程的输入。如果您的代码有InputStream,您可以从中读取- 这意味着它是其他进程的输出或错误。

Fortunately, the compiler will tell you if you're doing the wrong thing - you've got the data you want to provde, so you've got to write it to the stream, which means it's gotto be the OutputStream.

幸运的是,编译器会告诉你,如果你在做错误的事情-你已经得到了你想要provde数据,因此你将它写入流,这意味着它OutputStream

回答by Jonathon Faust

The getOutputStream is input tothe process. The getInputStream is output read fromthe process.

所述的getOutputStream被输入该过程。getInputStream 是进程中读取的输出。

Refer to the JavaDocsif it's helpful.

如果有帮助,请参阅JavaDocs

回答by Gaetan

Look at the doc. This is indeed completely messing around, compared to other framework that make it in the "obvious" way, so the doc is your friend.

看文档。与其他以“明显”方式实现的框架相比,这确实完全是一团糟,因此文档是您的朋友。

public abstract OutputStream getOutputStream()
> Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process

Should have been too simple to have:

应该太简单了:

public abstract InputStream getInputStream()
> Gets the standarinput stream of the subprocess. Output to the stream is piped into the standard input stream of the process