java读取MAC地址

来源:互联网 发布:播放器软件 编辑:程序博客网 时间:2024/05/16 09:14

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

}
 

原创粉丝点击