黑马程序员-JAVA学习第7课-Java程序与其它进程的数据通信
2011-05-25 00:03
453 查看
在Java程序中可以用Process类的实例对象来表示子进程,子进程的标准输入秋输出不再连接到键盘和显示器,而是以管道流的形式连接到父进程的一个输出流和输入流。
调用Process类的getOutputStream和getInputStream方法可以获得连接到子进程的输出流和输入流对象。
编程实例:在TestInOut类中启动java.exe命令执行另一个MyTest类,TestInOut和MyTest通过进程间的管道相互传递数据。
=================================================
MyTest.java
import java.io.*;
public class MyTest {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
BufferedReader bfr=new BufferedReader (
new InputStreamReader(System.in));
while(true)
{
String strLine=bfr.readLine();
if (strLine!=null)
System.out.println("hi:"+strLine);
else
break;
}
}
}
//---------------------------------------------
TestInOut.java
import java.io.*;
public class TestInOut implements Runnable {
Process p=null;
public TestInOut()
{
try{
p=Runtime.getRuntime().exec("java MyTest");
new Thread(this).start();
}catch(Exception e){
e.printStackTrace() ;
}
}
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
TestInOut tio=new TestInOut();
tio.send();
}
/**
* Method run
*
*
*/
public void send() //发送给子进程数据
{
try{
// int count=0;
OutputStream ops=p.getOutputStream();
while(true)
{
//System.out.println(++count);
ops.write("help/r/n".getBytes());
}
}catch (Exception e){
e.printStackTrace();
}
}
public void run()
{
try{
InputStream in=p.getInputStream();
BufferedReader bfr=new BufferedReader(
new InputStreamReader(in));
while(true)
{
String strLine=bfr.readLine();
if(strLine!=null)
{
System.out.println(strLine);
}
else
{ break;}
}
}catch(Exception e)
{
e.printStackTrace() ;
}
}
}
记住调用Process类的destroy方法结束子进程的运行。
调用Process类的getOutputStream和getInputStream方法可以获得连接到子进程的输出流和输入流对象。
编程实例:在TestInOut类中启动java.exe命令执行另一个MyTest类,TestInOut和MyTest通过进程间的管道相互传递数据。
=================================================
MyTest.java
import java.io.*;
public class MyTest {
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) throws Exception {
// TODO: 在这添加你的代码
BufferedReader bfr=new BufferedReader (
new InputStreamReader(System.in));
while(true)
{
String strLine=bfr.readLine();
if (strLine!=null)
System.out.println("hi:"+strLine);
else
break;
}
}
}
//---------------------------------------------
TestInOut.java
import java.io.*;
public class TestInOut implements Runnable {
Process p=null;
public TestInOut()
{
try{
p=Runtime.getRuntime().exec("java MyTest");
new Thread(this).start();
}catch(Exception e){
e.printStackTrace() ;
}
}
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
// TODO: 在这添加你的代码
TestInOut tio=new TestInOut();
tio.send();
}
/**
* Method run
*
*
*/
public void send() //发送给子进程数据
{
try{
// int count=0;
OutputStream ops=p.getOutputStream();
while(true)
{
//System.out.println(++count);
ops.write("help/r/n".getBytes());
}
}catch (Exception e){
e.printStackTrace();
}
}
public void run()
{
try{
InputStream in=p.getInputStream();
BufferedReader bfr=new BufferedReader(
new InputStreamReader(in));
while(true)
{
String strLine=bfr.readLine();
if(strLine!=null)
{
System.out.println(strLine);
}
else
{ break;}
}
}catch(Exception e)
{
e.printStackTrace() ;
}
}
}
记住调用Process类的destroy方法结束子进程的运行。
相关文章推荐
- 黑马程序员--java程序与其它进程的数据通信
- 第7讲 IO/输入与输出-Java程序与其它进程的数据通信
- Java程序与其它进程的数据通信
- (转)Java程序与其它进程的数据通信
- 8 Java程序与其它进程的数据通信
- Java程序与其它进程的数据通信
- java 程序与其它进程的数据通信
- JAVA高级视频_IO输入与输出 Java程序与其他进程的数据通讯 学习笔记
- JAVA程序与其他进程的数据通信
- Java IO 系列----Java程序与其他进程的数据通信
- IO 输入与输出(9)-- Java程序与其他进程的数据通信
- java程序与其他进程的数据通信
- 黑马程序员--Java学习03--Java程序的基本成分、数据类型
- [疯狂Java]I/O:标准流重定向、JVM读写其它进程数据
- 黑马程序员之ADO.NET学习笔记:省市选择程序,数据全部来自于数据库。
- 用C#调用Windows API和其它进程通信及C# 获得另外一程序 句柄 后控制该程序 控制其透明 窗体大小
- 黑马程序员-JAVA学习第7课--包装类
- 黑马程序员-JAVA学习之用多线程实现简单UDP聊天程序
- 黑马程序员—11—java基础:有关线程通信的学习笔记和学习心得体会
- 黑马程序员-学习笔记1-java程序的特性