In Java, the Process class provides the following 3 methods for communicating between programs and other child processes.
InputStream Geterrorstream (): Gets the error stream for the child process.
InputStream getInputStream (): Gets the input stream of the child process.
OutputStream Getoutputstream (): Gets the output stream of the child process.
The following program demonstrates reading output information from other processes:
ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader; Public classreadfromprocess { Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubProcess p=runtime.getruntime (). EXEC ("Javac");//run the Javac command to return to the child process that ran the command p Try { //Create a Bufferedreaderd object with the error stream of P process, which is the input stream of this program, and the output stream for P processBufferedReader br=NewBufferedReader (NewInputStreamReader (P.geterrorstream ())); String Buff=NULL; while((Buff=br.readline ())! =NULL) {//reads each row of data and assigns the buff string. System.out.println (Buff); } }Catch(IOException IoE) {ioe.printstacktrace (); } }}
Javac Process------------> this program
The data flows from the P process to the memory where the program resides
The program output results are as follows:
For the P process, the data flow is the output stream, and for this program it is the input stream.
Determine the angle of the input stream or the output stream: Always stand at the angle of the memory where the program is running.
Java Virtual machine reads and writes data from other processes