在物联网开发中经常需要获取本机的网卡信息,代码如下
1.获取mac
public static String getLocalMacAddress() {
String mac = "";
try {
String path = "sys/class/net/eth0/address";
FileInputStream fis_name = new FileInputStream(path);
byte[] buffer_name = new byte[1024 * 8];
int byteCount_name = fis_name.read(buffer_name);
if (byteCount_name > 0) {
mac = new String(buffer_name, 0, byteCount_name, "utf-8");
}
if (mac.length() == 0) {
path = "sys/class/net/eth0/wlan0";
FileInputStream fis = new FileInputStream(path);
byte[] buffer = new byte[1024 * 8];
int byteCount = fis.read(buffer);
if (byteCount > 0) {
mac = new String(buffer, 0, byteCount, "utf-8"