繁体   English   中英

将InetAddress转换为字符串

[英]converting InetAddress to string

我正在使用哈希表保存文件名(字符串)和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?

这是一个小程序,用于检查您想要的内容。 相同的代码,您共享的代码,效果很好。

 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM