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"); } }
java调用dos命令
猜你喜欢
转载自panyongzheng.iteye.com/blog/1473081
今日推荐
周排行