Read and Write data from other processes on the Java Virtual Machine

Source: Internet
Author: User

The exec () method of the Runtime object can be used to obtain the Process objects of other processes. The Process object represents the sub-processes started by the Java program. The Process class provides the following three methods, used to allow the program to communicate with its child processes.

InputStream getErrorStream (): gets the error stream of the sub-process

InputStream getInputStream (): gets the input stream of the sub-process.

OutputStream getOutputStream (): gets the output stream of the sub-process.

The following code implements error output for obtaining sub-Processes

[Java]
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
 
 
Public class Test {
Public static void main (String [] args) throws Exception
{

Process p;runtime.getruntime(cmd.exe c ("adb ");
BufferedReader br = new BufferedReader (new InputStreamReader (p. getErrorStream ()));
String str = null;
While (str = br. readLine ())! = Null)
{
System. out. println (str );
}
 
}

 
 

}

Import java. io. BufferedReader;
Import java. io. InputStreamReader;


Public class Test {
Public static void main (String [] args) throws Exception
{

Process p;runtime.getruntime(cmd.exe c ("adb ");
BufferedReader br = new BufferedReader (new InputStreamReader (p. getErrorStream ()));
String str = null;
While (str = br. readLine ())! = Null)
{
System. out. println (str );
}

}
 


 
}
The following program demonstrates the communication between two Java programs.

This number of parent Processes

[Java]
Import java. io. OutputStream;
Import java. io. PrintStream;
 
 
Public class Test {
Public static void main (String [] args) throws Exception
{
Process p;runtime.getruntime(cmd.exe c ("java work ");
OutputStream OS = p. getOutputStream ();
PrintStream ps = new PrintStream (OS );
Ps. println ("translated ");
OS. close ();


}

 
 

}

Import java. io. OutputStream;
Import java. io. PrintStream;


Public class Test {
Public static void main (String [] args) throws Exception
{
Process p;runtime.getruntime(cmd.exe c ("java work ");
OutputStream OS = p. getOutputStream ();
PrintStream ps = new PrintStream (OS );
Ps. println ("translated ");
OS. close ();


}
 


 
}

Below are the sub-Processes

[Java]
Import java. io. FileOutputStream;
Import java. io. PrintStream;
Import java. util. collections;
 
Public class work {
Public static void main (String [] args) throws Exception {
Pipeline SC = new pipeline (System. in );
FileOutputStream FCM = new FileOutputStream ("work ");
PrintStream ps = new PrintStream (FCM );
System. setOut (ps );
While (SC. hasNextLine ())
{
System. out. println (SC. nextLine ());
}
Ps. close ();
 
}
 
}

Import java. io. FileOutputStream;
Import java. io. PrintStream;
Import java. util. collections;

Public class work {
Public static void main (String [] args) throws Exception {
Pipeline SC = new pipeline (System. in );
FileOutputStream FCM = new FileOutputStream ("work ");
PrintStream ps = new PrintStream (FCM );
System. setOut (ps );
While (SC. hasNextLine ())
{
System. out. println (SC. nextLine ());
}
Ps. close ();

}

}

 

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.