java中如何执行本地程序并捕获out和err输出

类别:Java 点击:0 评论:0 推荐:
前些天机房中了病毒,jcreator坏了,editplus也用不起来那时只好用批处理给学生上课,想自己写个简单点的编译方式终于知道如何捕获输出了,学期也结束了:

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

/**
* Created on 2004-5-23
* @author yuchifang
* 测试如何执行本地程序并捕获out和err输出
*/
public class TestInOutErr
{
  public static void main(String[] args) throws IOException
  {
    Process p = Runtime.getRuntime().exec("java test");
    BufferedReader in =
              new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
    String currentLine = null;
    while ((currentLine = in.readLine()) != null)
      System.out.println(currentLine);
    BufferedReader err =
              new BufferedReader(
                    new InputStreamReader(p.getErrorStream()));
    while ((currentLine = err.readLine()) != null)
      System.out.println(currentLine);
  }
}

本文地址:http://com.8s8s.com/it/it16054.htm