## InetAddress类
InetAddress类用于获取网络上的硬件资源,互联网协议 (IP)地址,如ip地址、计算机名称
~~~java
/*
* InetAddress类的常用方法
*/
public class Test01 {
public static void main(String[] args) throws UnknownHostException {
// 获取本机的InetAddress实例,直接输出的是本机名和ip地址
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
System.out.println("计算机名:" + address.getHostName() + " ip地址:"
+ address.getHostAddress());
byte[] bytes = address.getAddress();// 获取字节数组形式的IP地址
System.out.println("字节数组形式的IP:" + Arrays.toString(bytes));
// 根据机器名和ip地址信息获取InetAddress实例
InetAddress address2 = InetAddress.getByName("hj-PC");
}
}
~~~