JAVA中怎样取得本机的MAC地址,或许windows取得IP地址比较简单,但是 linux呢? 其实原理是一样的,都是去读取输入流,下面提供了方法,也许比较笨,呵呵! 但是谁有 更好的大家拿出来奉献一下:
 
 
public static String getWindowsMACAddress() {
    String address = "";
    try {
     ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");
     Process p = pb.start();
     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
     String line;
     while ((line = br.readLine()) != null) {
        if (line.indexOf("Physical Address") != -1) {
         int index = line.indexOf(":");
         address = line.substring(index + 1);
         break;
        }
     }
     br.close();
     return address.trim();
    } catch (IOException e) {
    }
    return address;
}
public static String getLinuxMACAddress() {
    String address = "";
    try {
     ProcessBuilder pb = new ProcessBuilder("ifconfig", "-a");
     Process p = pb.start();
     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
     String line;
     while ((line = br.readLine()) != null) {
        System.out.println(line);
        if (line.indexOf("Link encap:Ethernet    HWaddr") != -1) {
         int index = line.indexOf("HWaddr");
         address = line.substring(index + 7);
         break;
        }
     }
     br.close();
     return address.trim();
    } catch (IOException e) {
    }
    return address;
}
public static String getMACAddress() {
    String address = "";
    String os = System.getProperty("os.name");
    // System.out.println(os);
    if (os != null && os.startsWith("Windows")) {
     address = getWindowsMACAddress();
     address = address.replaceAll("-", ":");
    } else {
     address = getLinuxMACAddress();
    }
    return address;
}
public static void main(String[] args) {
    System.out.println("取得网卡的 Mac 地址" + getMACAddress());
}