java获取网卡物理地址

package otherexample;

 

import java.net.NetworkInterface;
import java.util.Enumeration;

public class Cpu_Mac {
 
 public static void main(String[] args) {
  System.out.print(getMac()); 
 }
 protected static   String   toHex(byte   buf)   {
  int   n   =   buf   >=   0   ?   buf   :   256   +   buf;
  String   str   =   Integer.toHexString(n);
  return   str.toUpperCase();
  }
 private static String getMac() {
     try {
        Enumeration el = NetworkInterface.getNetworkInterfaces();
       while (el.hasMoreElements()) {
         byte[] mac = el.nextElement().getHardwareAddress();
         if (mac == null)
           continue;

          StringBuilder builder = new StringBuilder();
         for (byte b : mac) {
            builder.append(toHex(b));
            builder.append("-");
          }
          builder.deleteCharAt(builder.length() - 1);
         return builder.toString();

        }
      }
     catch (Exception exception) {
        exception.printStackTrace();
      }
     return null;
    }

}

你可能感兴趣的:(Java实用功能源码集锦)