我使用哈希表来保存文件名(字符串)和InetAddress
Hashtable <String , InetAddress > file_location = new Hashtable <String , InetAddress >();
我正在使用它来检索地址,但我只返回一个空值
file_location.put("ABD_9158" , IPAddress); //IPAdress is of InetAddress type
InetAddress n = file_location.get("ABD_9158");
System.out.println(n);
尝试将n更改为字符串,但是还没找到 我的问题,如何检索ipaddress?
答案 0 :(得分:0)
这是一个小程序,可以检查你想要什么。相同的代码,你分享的内容,它工作得很好。
public static void main(String args[]) {
Hashtable<String, InetAddress> fileLocation = new Hashtable<String, InetAddress>();
InetAddress addr;
try {
addr = InetAddress.getByName("127.0.0.1");
fileLocation.put("ABD_9158", addr); // IPAdress is of InetAddress type
InetAddress n = fileLocation.get("ABD_9158");
System.out.println(n);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
<强>输出强>
/127.0.0.1