Java Virtual machine reads and writes data from other processes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.