获取mac地址又一方法

Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            byte[] hardwareAddress = networkInterface.getHardwareAddress();


            if (hardwareAddress != null) {
                for (int i = 0; i < hardwareAddress.length; i++) {
                    System.out.print(String.format("%x", hardwareAddress[i]));
                    if (i != hardwareAddress.length - 1) {
                        System.out.print(":");
                    }
                }
                System.out.println();
            }
        }
以前的方法

public static String[] getMacs() throws IOException {
	Runtime runtime = Runtime.getRuntime();
	List<String> alMac = new ArrayList<String>();
	BufferedReader rd = null;
	try {
		Process process = runtime.exec("ipconfig /all");//这种方法不能跨os, 只能根据不同的os写不同的命令
		InputStream is = process.getInputStream();
		rd = new BufferedReader(new InputStreamReader(is));
		String line = null;
		while ((line = rd.readLine()) != null) {
			if (line.indexOf("物理地址") > -1 || line.toLowerCase().startsWith("physical address")) {
				int index = line.lastIndexOf(":");
				if (line.length() - 1 >= index && index > -1) {
					String mac = line.substring(index + 1);
					alMac.add(mac);
				}
			}
		}
	} finally {
		if (rd != null) rd.close();
	}
	return alMac.toArray(new String[0]);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值