java调用bat文件

package com.doing.utils;

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

public class BatUtils {

            /**
      * 后台调用bat 尽量不要使用中文路径
      * @param batFileName  bat文件路径
      */
            public static void  callCmd(String batFileName) {
                StringBuilder sb = new StringBuilder();
                try {
                        Process        child          = Runtime.getRuntime().exec(batFileName);
                        InputStream    in             = child.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                        String         line;
                        while ((line = bufferedReader.readLine()) != null) {
                                sb.append(line + "\n");
                            }
                        in.close();
                        try {
                                child.waitFor();
                            } catch (InterruptedException e) {
                                System.out.println(e);
                            }
                        child.destroy();
                        System.out.println("sb:" + sb.toString());
                        System.out.println("callCmd execute finished");
                    } catch (IOException e) {
                        System.out.println(e);
                    }
            }
}

你可能感兴趣的:(jAVA工具,java)