六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 78|回复: 0

java操作DOS命令

[复制链接]

升级  9.33%

66

主题

66

主题

66

主题

举人

Rank: 3Rank: 3

积分
228
 楼主| 发表于 2013-1-15 02:14:27 | 显示全部楼层 |阅读模式
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);
    }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表