搜档网
当前位置:搜档网 › 获取网卡的MAC地址代码

获取网卡的MAC地址代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class mac {
public static String getWindowsMACAddress()
{
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null) {
if (line.indexOf("以太网适配器")>=0) //寻找标示字符串以太网适配器
{
while ((line = bufferedReader.readLine()) != null){
index = line.indexOf("物理地址");// 寻找标示字符串【物理地址】
if (index >= 0) {// 找到了
index = line.indexOf(":"); // 寻找":"的位置
if (index>=0) {
mac = line.substring(index + 1).trim();// 取出mac地址并去除2边空格
break;
} } } } }
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
bufferedReader = null;
process = null;
}
return mac;
}
public static void main(String[] argc) {
String mac = getWindowsMACAddress();
System.out.println("MAC地址是:"+mac);
}
}

相关主题