JAVA查询MAC地址问题

来源:百度知道 编辑:UC知道 时间:2024/05/06 01:41:57
package src;

import java.io.*;

public class getMac {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
if (line.indexOf("Physical Address") > 0) {
String MACAddr = line.substring(line.indexOf("-") - 2);
System.out.println("MAC address = [" + MACAddr + "]");
}
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());
}
}
}

这段程序什么意思呀?
Proce

ipconfig是Windows下命令提示符支持的一个命令,可以查询到你的机器的ip等网络配置

Runtime.getRuntime().exec("ipconfig /all"); 就是执行该命令

if (line.indexOf("Physical Address") > 0)表示如果在line中查找到Physical Address,就继续执行if中的语句,否则如果找不到,line.indexOf("Physical Address")的返回值=-1