zhaoshijie 发表于 2013-1-15 02:14:27

java操作DOS命令

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

public class Dos {
    public Dos() {
    }

    public static String getMACAddress() {

      String address = "";
      //获取系统类型,如:windows、Linux、Unix等;
      String os = System.getProperty("os.name");
      if (os != null && os.startsWith("Windows")) {
            try {
                String command = "cmd.exe /c ipconfig /all";
               
               
                Process p = Runtime.getRuntime().exec(command);
                System.out.println("command="+p.getOutputStream().toString());
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                  if (line.indexOf("Physical Address") > 0) {
                        int index = line.indexOf(":");
                        index += 2;
                        address = line.substring(index);
                        break;
                  }
                }
                br.close();
                return address.trim();
            } catch (IOException e) {
                e.printStackTrace();
            }
      }
      return address;
    }

   
   

   
    public static boolean command(String command) {
      boolean err = false;
      try {
      Process process =
      new ProcessBuilder(command.split(" ")).start();
      BufferedReader results = new BufferedReader(
      new InputStreamReader(process.getInputStream()));
      String s;
      while((s = results.readLine())!= null)
      System.out.println(s);
      BufferedReader errors = new BufferedReader(
      new InputStreamReader(process.getErrorStream()));
      // Report errors and return nonzero value
      // to calling process if there are problems:
      while((s = errors.readLine())!= null) {
      System.err.println("错误:"+s);
      err = true;
      return false;
      }
      } catch(Exception e) {
      // Compensate for Windows 2000, which throws an
      // exception for the default command line:
      if(!command.startsWith("CMD /C"))
      command("CMD /C " + command);
      else
      throw new RuntimeException(e);
      }
      if(err){
      
      }
      return true;
    }
   
   
    public static void main(String[] args) {
      
      boolean isOk = Dos.command("svnadmin create d:\\BPMHOME\\B");
      
      System.out.println(isOk);
    }
}
页: [1]
查看完整版本: java操作DOS命令