获取hosts文件中的信息
实际开发过程中,有时候需要获取系统hosts文件中的信息
可以根据java提供的InetAddress类获取信息
/**
* @throws Exception
* @描述 从hosts文件中读取配置信息
* @date 2020-07-15
* <p>
* 1 在hosts中配置 127.0.0.1 zuul6001.com
* 2 利用下面代码获取
*/
public static void hostsValue() throws Exception {
String testURL = "zuul6001.com";
System.out.println(InetAddress.getByName(testURL));
InetAddress inetAddress = InetAddress.getByName(testURL);
String hostName = inetAddress.getHostName();
String hostAddress = inetAddress.getHostAddress();
System.out.println("hostName=" + hostName);
System.out.println("hostAddress=" + hostAddress);
}
运行输出结果:
zuul6001.com/127.0.0.1
hostName = zuul6001.com
hostAddress = 127.0.0.1
本文来自博客园,作者:zhangpba,转载请注明原文链接:https://www.cnblogs.com/zhangpb/p/13305562.html