java调用dos命令
package com;import java.io.BufferedReader;import java.io.InputStreamReader;public class CommonUtils {public static String exeCMD1(String cmd){cmd = "cmd.exe /c "+cmd;Process process = null;StringBuffer sb = new StringBuffer();BufferedReader reader = null;try {process = Runtime.getRuntime().exec(cmd);reader = new BufferedReader(new InputStreamReader(process.getInputStream()));while(true){String str = reader.readLine();if(str==null) break;System.out.println(str);sb.append(str);}process.destroy();} catch (Exception e) {// TODO: handle exception}return sb.toString();}public static String exeCMD2(String cmd){String[] args = new String[]{"cmd","/c",cmd}; Process process = null;StringBuffer sb = new StringBuffer();BufferedReader reader = null;try {process = Runtime.getRuntime().exec(args);reader = new BufferedReader(new InputStreamReader(process.getInputStream()));while(true){String str = reader.readLine();if(str==null) break;System.out.println(str);sb.append(str);}process.destroy();} catch (Exception e) {// TODO: handle exception}return sb.toString();}public static void main(String[] args){CommonUtils.exeCMD1("tree");CommonUtils.exeCMD1("tree");}}