现在的位置: 首页 > 综合 > 正文

java读取MAC地址

2013年09月17日 ⁄ 综合 ⁄ 共 711字 ⁄ 字号 评论关闭

package com.lxh.test;

import java.io.*;
/*
 * lxh
 */

public class Test {
 public static void main(String[] args) throws Exception  {
  String line;
  String physicalAddress = "read MAC error!";
  try {
   Process p=Runtime.getRuntime().exec("cmd.exe /c ipconfig /all");
   //p.waitFor();
   BufferedReader bd =new BufferedReader(new InputStreamReader (p.getInputStream()));
   while((line=bd.readLine())!=null){
    if(line.indexOf("Physical Address. . . . . . . . . :")!=-1){
     if(line.indexOf(":")!=-1){
      physicalAddress = line.substring(line.indexOf(":")+2);
      break; //找到MAC,推出循环
     }
    }
   }
   p.waitFor();
   System.out.println("信息:"+line+"/n");
   System.out.println("本机的MAC地址是: "+ physicalAddress);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}
 

抱歉!评论已关闭.