Java: convert InetAddress to string
•
Java
I searched a lot, but I couldn't find any way to convert InetAddress type to string (maybe my search was not very good: s) I have to convert it to string type because I have to display the result component in textarea (GUI), which requires string type Anyone can. Is that possible?
Solution
The Java API for InetAddress class has a good method to meet your needs. I see
public static void main ( String [] args ) throws UnkNownHostException { // Instantiate your own InetAddress object. InetAddress address = InetAddress.getLocalHost(); String hostIP = address.getHostAddress() ; String hostName = address.getHostName(); System.out.println( "IP: " + hostIP + "\n" + "Name: " + hostName); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码