首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

java调用dos下令

2014-07-08 
java调用dos命令package com;import java.io.BufferedReader;import java.io.InputStreamReader;public cl

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");}}

热点排行