java取mac地址

网上搜到一段取mac的代码,刚好用到,存下,改进使用正则表达式来匹配mac

获取mac地址
public String getMACAddress(String ip) {
String str
= "";
String macAddress
= "";
try {
Process p
= Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStream inpput
= p.getInputStream();
String content
= IOUtils.toString(inpput);

Pattern pattern
= Pattern.compile("\\w{2}-\\w{2}-\\w{2}-\\w{2}-\\w{2}-\\w{2}");
Matcher matcher
= pattern.matcher(content);

if (matcher.find()) {
macAddress
= matcher.group();
}
}
catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
posted @ 2010-02-06 11:04  雪霁霜飞  阅读(253)  评论(0编辑  收藏  举报