如果使用上述三个Getter方法。
package mynet;
import java.net.*;
import java.util.*;
public class MyNetworkInterface4
{
public static void main(String[] args) throws Exception
{
if (args.length == 0)
return;
NetworkInterface ni = NetworkInterface.getByName(args[0]);
System.out.println("Name: " + ni.getName());
System.out.println("DisplayName: " + ni.getDisplayName());
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements())
System.out.println(addresses.nextElement().getHostAddress());
}
}
测试
执行如下命令:
java mynet.MyNetworkInterface4 eth0
运行结果:
Name: eth0
DisplayName: Realtek RTL8139 Family PCI Fast Ethernet NIC
192.168.18.10
192.168.18.20
|