现在的位置: 首页 > 综合 > 正文

java 调用bat文件

2014年02月13日 ⁄ 综合 ⁄ 共 1059字 ⁄ 字号 评论关闭

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {

 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  // TODO Auto-generated method stub
  Process p = Runtime.getRuntime().exec("cmd /c D:\\a.bat");
    InputStream fis = p.getInputStream();
    final BufferedReader br2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    InputStreamReader isr = new InputStreamReader(fis);
    final BufferedReader br = new BufferedReader(isr);
    Thread t1 = new Thread(){
     public void run(){
     String line = null;
     try {
      while ((line = br2.readLine()) != null) {
       System.out.println(line);
      }
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     }
    };
    Thread t2 = new Thread(){
     public void run(){
     String line = null;
     try {
      while ((line = br.readLine()) != null) {
       System.out.println(line);
      }
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     }
    };
    t1.start();
    t2.start();

 }

}

抱歉!评论已关闭.