标签:str main get array system public test localhost throw
一、InetAddress:互联网协议 (IP) 地址;java.net包;
二、获取对象方法:
//没有构造方法;
(1)static InetAddress getLocalHost();获取本机IP地址;//InetAddress lh = InetAddress.getLocalHost();
(2)static InetAddress getByName(String host);根据主机名获取IP地址;
(3)static InetAddress[] getAllByName(String host);根据主机名获取多个IP地址;
public class Test { public static void main(String[] args) throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost); InetAddress byName = InetAddress.getByName("DESKTOP"); System.out.println(byName); InetAddress[] allByName = InetAddress.getAllByName("DESKTOP"); System.out.println(Arrays.toString(allByName)); } }
三、方法:
(1)String getHostName();获取此IP的主机名;//lh.getHostName();
(2)String getHostAddress();获取IP的字符串类型;
public class Test { public static void main(String[] args) throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost.getHostName()); //DESKTOP System.out.println(localHost.getHostAddress()); //192.168.0.0 } }
标签:str main get array system public test localhost throw
原文地址:https://www.cnblogs.com/Tractors/p/11259464.html