读书人

Process输入流输出流的准确使用

发布时间: 2012-09-06 10:37:01 作者: rapoo

Process输入流输出流的正确使用

package aa;import java.io.*;public class TestDemo{  Process pc;Runtime rt;public TestDemo() throws Exception{  rt=Runtime.getRuntime();  String [] ss={"E:\\work\\Demo\\src\\Test.exe"};  pc=rt.exec(ss);   new Thread(new Input()).start();  Thread.sleep(500);  new Thread(new Output()).start();}public static void main(String args[]) throws Exception{new TestDemo();}class Output implements Runnable{public void run(){          OutputStream fos=pc.getOutputStream();      PrintStream ps=new PrintStream(fos);      ps.print("another\n");  System.out.println("已经输出");  ps.flush();  }}    class Input implements Runnable    {public void run(){     InputStream ios=pc.getInputStream(); BufferedReader brd=new BufferedReader(new InputStreamReader(ios)); String s; try{     while((s=brd.readLine())!=null){ System.out.println(s);      } }catch(Exception e){ }    pc.destroy();}    }}

读书人网 >编程

热点推荐