InetAddress 类的方法
//此类中没有定义构造器,是通过静态方法返回此类的对象实例
static InetAddress getByAddress(byte[] addr)
在给定原始 IP 地址的情况下,返回 InetAddress 对象。
static InetAddress getByAddress(String host, byte[] addr)
根据提供的主机名和 IP 地址创建 InetAddress。
static InetAddress getByName(String host)
在给定主机名的情况下确定主机的 IP 地址。
static InetAddress getLocalHost()
返回本地主机。
String getHostAddress()
返回 IP 地址字符串(以文本表现形式)。
String getHostName()
获取此 IP 地址的主机名。
String toString()
将此 IP 地址转换为 String。
public class Example1 {
public static void main(String args[]) throws Exception {
InetAddress address;
address = InetAddress.getByName("www.baidu.com");
System.out.println(address);
address = InetAddress.getLocalHost();
System.out.println(address);
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());
System.out.println(address.getAddress());
}
}