Java虚拟机读写其他进程的数据
2012-08-22 15:51
295 查看
Java虚拟机读写其他进程的数据
http://axiangtaihe.iteye.com/blog/1647288
使用Runtime对象的exec()方法可以获得其他进程的Process对象,Process对象代表由该Java程序启动的子进程,Process类提供了如下3个方法,用于让程序和其子进程进行通讯。
InputStream getErrorStream():获取子进程的错误流
InputStream getInputStream():获取子进程的输入流
OutputStream getOutputStream():获取子进程的输出流
下面的代码实现了获取子进程的错误输出
Java代码
![](http://zpball.iteye.com/images/icon_copy.gif)
![](http://zpball.iteye.com/images/icon_star.png)
![](http://zpball.iteye.com/images/spinner.gif)
import java.io.BufferedReader; import java.io.InputStreamReader; public class Test { public static void main(String[] args) throws Exception { Process p=Runtime.getRuntime().exec("adb"); BufferedReader br=new BufferedReader(new InputStreamReader(p.getErrorStream())); String str=null; while((str=br.readLine())!=null) { System.out.println(str); } } }
下面程序演示两个Java程序通讯
这个数父进程
Java代码
![](http://zpball.iteye.com/images/icon_copy.gif)
![](http://zpball.iteye.com/images/icon_star.png)
![](http://zpball.iteye.com/images/spinner.gif)
import java.io.OutputStream; import java.io.PrintStream; public class Test { public static void main(String[] args) throws Exception { Process p=Runtime.getRuntime().exec("java work"); OutputStream os=p.getOutputStream(); PrintStream ps=new PrintStream(os); ps.println("张译成"); os.close(); } }
下面是子进程
Java代码
![](http://zpball.iteye.com/images/icon_copy.gif)
![](http://zpball.iteye.com/images/icon_star.png)
![](http://zpball.iteye.com/images/spinner.gif)
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class work {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
FileOutputStream fis=new FileOutputStream("work");
PrintStream ps=new PrintStream(fis);
System.setOut(ps);
while(sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
ps.close();
}
}
http://axiangtaihe.iteye.com/blog/1647288
使用Runtime对象的exec()方法可以获得其他进程的Process对象,Process对象代表由该Java程序启动的子进程,Process类提供了如下3个方法,用于让程序和其子进程进行通讯。
InputStream getErrorStream():获取子进程的错误流
InputStream getInputStream():获取子进程的输入流
OutputStream getOutputStream():获取子进程的输出流
下面的代码实现了获取子进程的错误输出
Java代码
![](http://zpball.iteye.com/images/icon_copy.gif)
![](http://zpball.iteye.com/images/icon_star.png)
![](http://zpball.iteye.com/images/spinner.gif)
import java.io.BufferedReader; import java.io.InputStreamReader; public class Test { public static void main(String[] args) throws Exception { Process p=Runtime.getRuntime().exec("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().exec("adb"); BufferedReader br=new BufferedReader(new InputStreamReader(p.getErrorStream())); String str=null; while((str=br.readLine())!=null) { System.out.println(str); } } }
下面程序演示两个Java程序通讯
这个数父进程
Java代码
![](http://zpball.iteye.com/images/icon_copy.gif)
![](http://zpball.iteye.com/images/icon_star.png)
![](http://zpball.iteye.com/images/spinner.gif)
import java.io.OutputStream; import java.io.PrintStream; public class Test { public static void main(String[] args) throws Exception { Process p=Runtime.getRuntime().exec("java work"); OutputStream os=p.getOutputStream(); PrintStream ps=new PrintStream(os); ps.println("张译成"); 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().exec("java work"); OutputStream os=p.getOutputStream(); PrintStream ps=new PrintStream(os); ps.println("张译成"); os.close(); } }java电子书免费下载
下面是子进程
Java代码
![](http://zpball.iteye.com/images/icon_copy.gif)
![](http://zpball.iteye.com/images/icon_star.png)
![](http://zpball.iteye.com/images/spinner.gif)
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class work {
public static void main(String[] args) throws Exception{
Scanner sc=new Scanner(System.in);
FileOutputStream fis=new FileOutputStream("work");
PrintStream ps=new PrintStream(fis);
System.setOut(ps);
while(sc.hasNextLine())
{
System.out.println(sc.nextLine());
}
ps.close();
}
}
相关文章推荐
- 15.6 Java虚拟机读写其他进程的数据
- Java虚拟机读写其他进程的数据--Process对象
- Java虚拟机读写其他进程的数据
- Java虚拟机读写其他进程的数据
- JAVA虚拟机读写其他进程数据
- IO:Java虚拟机读写其他进程的数据(1)
- JAVA输入输出(6)--Java虚拟机读写其他进程的数据
- 15.6 Java虚拟机读写其他进程的数据
- Java虚拟机访问读写其他进程的数据--RandomAccessFile
- java虚拟机读写其它进程的数据及RandomAccessFile
- IO:Java虚拟机读入其他进程的数据(2)
- 由于您和其他用户试图同时改变同一数据,导致 Microsoft Jet 数据库引擎停止进程解决办法
- IO 输入与输出(9)-- Java程序与其他进程的数据通信
- 读取其他进程内存数据
- windows api学习笔记-读写其他进程的内存
- android 查看其他进程内存数据
- 获取其他进程Listctrl的数据
- VirtualAllocEx 跨进程读写数据 代码注入
- 修改其他进程的数据
- 读取其他进程的数据