Java Virtual machine reads and writes data from other processes--process objects

Source: Internet
Author: User

Using the Exec () method of the runtime object, you can run other programs on the platform that produce a process object that represents a child process that is started by the Java program.

The Process class provides 3 methods for communicating a program to its child processes

InputStream Geterrorstream (): Gets the error stream for the child process.

InputStream Getinputsteeam (): Gets the input stream of the child process.

OutputStream Getoutputstream (): Gets the output stream of the child process.

Example: Reading output information from another process

 PackageCom.chengxuyuanzhiliu;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader; Public classreadfromprocess { Public Static voidMain (string[] args)throwsIOException {Process P= Runtime.getruntime (). EXEC ("Javac"); //try () {}jdk7 new syntax, closes the stream in (), effect type in finally Br.close ()        Try(BufferedReader br =NewBufferedReader (NewInputStreamReader (P.geterrorstream ()))) {String Buff=NULL;  while(Buff = Br.readline ())! =NULL) {System.out.println (buff); }        }    }}

Execution Result:

Similar to the output information example for other processes

Java Virtual machine reads and writes data from other processes--process objects

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.