获取本机ip地址以及主机名称


package guyu.day0824;

import java.net.InetAddress;

/**
* @Author: LHT
* @Date: 2020/8/24 09:39
*/
public class Demo01 {
public static void main(String[] args) throws Exception {

//使用 InetAddress 类的 getLocalAddress() 方法获取本机ip地址及主机名
InetAddress addr = InetAddress.getLocalHost();
System.out.println("Local HostAddress: "+addr.getHostAddress());
String hostname = addr.getHostName();
System.out.println("Local host name: "+hostname);
}
}

posted @ 2020-08-24 10:06  瓦刀哥  阅读(301)  评论(0编辑  收藏  举报