Java 输入/输出——Java虚拟机读写其它进程的数据

  使用 Runtime对象的exec()方法可以运行平台上的其它程序,该方法产生一个Process对象,Process对象代表由该Java程序启动的子进程。Process类提供了如下三个方法,用于让程序和其子进程进行通信。

abstract InputStream getErrorStream​()
Returns the input stream connected to the error output of the process.(获取子进程的错误流)
abstract InputStream getInputStream​()
Returns the input stream connected to the normal output of the process.(获取子进程的输入流)                                                                     
abstract OutputStream getOutputStream​()
Returns the output stream connected to the normal input of the process.(获取子进程的输出流)
 1 package com.zyjhandsome.io;
 2 
 3 import java.io.*;
 4 
 5 public class ReadFromProcess {
 6 
 7     public static void main(String[] args) throws IOException {
 8         // 运行javac命令,返回运行该命令的子进程
 9         // 使用Runtime启动了javac程序,获得了运行该程序对应的子进程
10         Process p = Runtime.getRuntime().exec("javac");
11         try {
12             // 以p进程的错误流创建BufferedReader对象
13             // 这个错误流对本程序是输入流,对p进行是输出流
14             BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
15             String buff = null;
16             // 采取循环方式来读取p进程的错误输出
17             while ((buff = br.readLine()) != null)
18             {
19                 System.out.println(buff);                
20             }
21         } catch (Exception e) {
22             // TODO Auto-generated catch block
23             e.printStackTrace();
24         }
25     }
26 }

   不仅如此,也可以通过Process的getOutputStream()方法获得向进程输入数据的流(该进程对Java程序是输出流,对子进程是输入流),如下程序实现了在Java程序中启动Java虚拟机运行另一个 Java程序,并向另一个Java程序中输入数据。

 1 package com.zyjhandsome.io;
 2 
 3 import java.io.*;
 4 import java.util.*;
 5 
 6 public class WriteToProcess {
 7 
 8     public static void main(String[] args) throws IOException {
 9         // TODO Auto-generated method stub
10         // 运行java ReadStandard命令,返回运行该命令的子进程
11         Process p = Runtime.getRuntime().exec("java ReadStandard");
12         // 以p进程的输出流创建PrintStream对象
13         // 这个输出流对本程序是输出流,对p进程则是输入流
14         try {
15             PrintStream ps = new PrintStream(p.getOutputStream());
16             // 向ReadStandard程序写入内容,这些内容将被ReadStandard读取
17             ps.println("普通字符串");
18             ps.println(new WriteToProcess());
19             
20         } catch (Exception e) {
21             // TODO Auto-generated catch block
22             e.printStackTrace();
23         }        
24     }
25 }
26     
27 //定义一个ReadStandard类,该类可以接受标准输入,
28 //并将标准输入写入out.txt文件。
29 class ReadStandard
30 {
31     public static void main(String[] args)
32     {
33         try(
34             // 使用System.in创建Scanner对象,用于获取标准输入
35             Scanner sc = new Scanner(System.in);
36             PrintStream ps = new PrintStream(new FileOutputStream("D:\User_zhaoyingjun\JavaSE\Test\WriteToProcess.txt")))
37         {
38             // 增加下面一行将只把回车作为分隔符
39             sc.useDelimiter("
");
40             // 判断是否还有下一个输入项
41             while(sc.hasNext())
42             {
43                 // 输出输入项
44                 ps.println("键盘输入的内容是:" + sc.next());
45             }
46         }
47         catch(IOException ioe)
48         {
49             ioe.printStackTrace();
50         }
51     }
52 }
原文地址:https://www.cnblogs.com/zyjhandsome/p/9696933.html