简体   繁体   English

将InetAddress转换为字符串

[英]converting InetAddress to string

Im using a hashtable to save filename (string) and InetAddress 我正在使用哈希表保存文件名(字符串)和InetAddress

Hashtable <String , InetAddress > file_location = new Hashtable <String , InetAddress >(); 

and im using this to retrieve the address, but im only getting a null value returned 我用它来获取地址,但是我只得到了一个空值

file_location.put("ABD_9158" , IPAddress); //IPAdress is of InetAddress type

 InetAddress n = file_location.get("ABD_9158");

        System.out.println(n);

tried changing n to a string but , havent been able to find away my question, how do retrieve the ipaddress ? 尝试将n更改为字符串,但是,还没有找到我的问题,如何检索ipaddress?

This is a small program, to check what you want. 这是一个小程序,用于检查您想要的内容。 Same piece of code, what you have shared, it is working fine. 相同的代码,您共享的代码,效果很好。

 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();
        }
    }

Output 输出量

/127.0.0.1

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

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