Java调用外部程序

import java.io.*;
public class RuntimeTest {
    public static void main(String[] args) throws IOException, InterruptedException {
        long begin = System.currentTimeMillis();
        //调用外部程序
        Process exec = Runtime.getRuntime().exec("ping 127.0.0.1");
        InputStream inputStream = exec.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"GBK"));
        String line = null;
        //读取外部程序的输出结果
        System.out.println("开始输出外部程序的返回数据");
        while(null!=(line=reader.readLine())){
            System.out.println(line);
        }
        System.out.println("耗时:"+(System.currentTimeMillis()-begin)+"ms");
    }
}

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注