I just took a look at the documentation of Commons Exec, and there is another solution to the pipeline problem you mentioned. The pipeline actually just connects the output stream of the previous program to the input stream of the following program. I read the documentation and found that Commons Exec provides an interface for redirecting process input and output streams. Commons Exec的文档,你所说的管道问题还有另外一个解决办法。管道实际上只是把前一个程序的输出流和后一个程序的输入流连接起来而已。我看了文档,Commons Exec提供了重定向进程输入输出流的接口。
Specifically, suppose there are two programs A and B, which were originally connected through pipeline commands (A | B). You can do this with Commons Exec:
First create an Executor object that runs program A, then obtain its ExecuteStreamHandler (through the getStreamHandler() method), and reset the object's The output stream is an input stream you define (either standard input or a file). (Note that the output stream of program A is actually an input stream object, that is, an InputStream object, such as standard input. This is because the output of the program is similar to us typing to standard input, so its goal is an input stream)🎜
🎜This is equivalent to you getting the output stream of program A. The key now is to assign this output stream to the input stream of program B. 🎜
🎜You can convert the output stream of program A (an InputStream object) obtained earlier into an OutputStream object (very simple, read from the former and directly send it to the latter Just write), and then assign this output stream object to the input stream of the Executor object of program B in the same way as program A. In this way, the input and output streams of programs A and B are connected. 🎜
🎜It is recommended to encapsulate this process, for example, it is called Pipe class, which is used to realize the pipeline connection between two processes. 🎜
I just took a look at the documentation of
Commons Exec
, and there is another solution to the pipeline problem you mentioned. The pipeline actually just connects the output stream of the previous program to the input stream of the following program. I read the documentation and found thatCommons Exec
provides an interface for redirecting process input and output streams.Commons Exec
的文档,你所说的管道问题还有另外一个解决办法。管道实际上只是把前一个程序的输出流和后一个程序的输入流连接起来而已。我看了文档,Commons Exec
提供了重定向进程输入输出流的接口。具体来说就是,假设有A、B两个程序,原本是通过管道命令连接在一起的(
A | B
)。用Commons Exec
可以这样做:先创建一个运行A程序的
Executor
对象,然后获取它的ExecuteStreamHandler
(通过getStreamHandler()
方法),重设该对象的输出流为你自己定义的一个输入流(标准输入或文件都可以)。(注意A程序的输出流实际上是一个输入流对象,即InputStream
对象,例如标准输入。这是因为程序的输出类似于我们向标准输入打字一样,所以其作用目标是一个输入流)这样就相当于你拿到了A程序的输出流,现在的关键是把这个输出流赋给B程序的输入流。
你可以自己把前面得到的A程序的输出流(一个
Specifically, suppose there are two programs A and B, which were originally connected through pipeline commands (InputStream
对象)转换为一个OutputStream
对象(很简单,从前者读取后直接向后者写入即可),然后按照与A程序相同的处理方式,把这个输出流对象赋给B程序的Executor
A | B
). You can do this withCommons Exec
:Executor
object that runs program A, then obtain itsExecuteStreamHandler
(through thegetStreamHandler()
method), and reset the object's The output stream is an input stream you define (either standard input or a file). (Note that the output stream of program A is actually an input stream object, that is, anInputStream
object, such as standard input. This is because the output of the program is similar to us typing to standard input, so its goal is an input stream)🎜 🎜This is equivalent to you getting the output stream of program A. The key now is to assign this output stream to the input stream of program B. 🎜 🎜You can convert the output stream of program A (anInputStream
object) obtained earlier into anOutputStream
object (very simple, read from the former and directly send it to the latter Just write), and then assign this output stream object to the input stream of theExecutor
object of program B in the same way as program A. In this way, the input and output streams of programs A and B are connected. 🎜 🎜It is recommended to encapsulate this process, for example, it is called Pipe class, which is used to realize the pipeline connection between two processes. 🎜