Java读取本地机器MAC地址

package org.liufei.jweb;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class MacAddress {

private static String hexByte(byte b) {
String s = "000000" + Integer.toHexString(b);
return s.substring(s.length() - 2);
}

public static String getHostMAC(String split) throws SocketException {
Enumeration<NetworkInterface> networkInterface_enumeration;
String mac_address = "";
networkInterface_enumeration = NetworkInterface.getNetworkInterfaces();
while (networkInterface_enumeration.hasMoreElements()) {
byte[] mac = networkInterface_enumeration.nextElement().getHardwareAddress();
if (mac == null)
continue;
mac_address = hexByte(mac[0]) + split + hexByte(mac[1]) + split
+ hexByte(mac[2]) + split + hexByte(mac[3]) + split
+ hexByte(mac[4]) + split + hexByte(mac[5]);
}
return mac_address;
}

public static void main(String[] args) throws SocketException {
System.out.println(MacAddress.getHostMAC(":"));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用多种库来读取本地JSON文件,其中最常用的是Fastjson和Jackson。 Fastjson是一个由阿里巴巴开发的高性能的JSON处理器,它支持将JSON字符串转换为Java对象,以及将Java对象转换为JSON字符串。Fastjson的使用非常方便,只需要引入相关的包,并使用JSON类中的parseObject方法读取JSON文件即可。示例代码如下所示: ``` import com.alibaba.fastjson.JSON; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; public class ReadJSONFile { public static void main(String[] args) throws IOException { String filePath = "D:/data.json"; byte[] bytes = Files.readAllBytes(Paths.get(filePath)); String jsonString = new String(bytes, StandardCharsets.UTF_8); JSON.parseObject(jsonString); } } ``` Jackson是一个广泛使用的JSON库,它可以实现JSON和Java对象之间的相互转换,以及JSON的读写操作。与Fastjson类似,使用Jackson读取本地JSON也很简单,只需使用ObjectMapper类实例化一个对象,并使用readValue方法读取JSON文件即可。示例代码如下所示: ``` import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class ReadJSONFile { public static void main(String[] args) throws IOException { String filePath = "D:/data.json"; ObjectMapper mapper = new ObjectMapper(); byte[] bytes = Files.readAllBytes(Paths.get(filePath)); mapper.readValue(bytes, Object.class); } } ``` 无论是使用Fastjson还是Jackson,读取本地JSON都非常方便。读取后,可以使用Java中的其他工具对JSON进行解析、操作或者序列化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值