2012-02-18 23 views
9

Çok arandım ama inetaddress türünü string'e dönüştürmenin herhangi bir yolunu bulamadım (aradığım şey bu kadar iyi değil: S) .Bunu string türüne dönüştürmek zorundayım çünkü ben sonuç dizge türü gerektiren textarea (gui bileşeni) görüntülemek zorunda.Yani bu nasıl mümkün olabilir ??Java: inetaddress - String çevirme

cevap

19

InetAddress sınıfı için Java API'sı ihtiyaçlarınız için iyi yöntemlere sahiptir, anladım. Bu yöntemleri deneyin. Diğer gereksinimleriniz için InetAddress'teki diğer mülk sahiplerine göz atabilirsiniz.

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

InetAddress.toString() yöntemini denediniz mi?

+0

Bu eğik çizgi döndürür. Bunun yerine getHostAddress() kullanın. Önemli bit için, http://stackoverflow.com/questions/12947435/inetaddress-tostring-returns-a-forward-slash – LeslieM

1

Ne

System.out.println(Inet4Address.getLocalHost().getHostAddress()); 

hakkında?

3

Yalnızca InetAddress toString() yöntemini çağırın. Ayrıca, özellikle ana bilgisayar adını istiyorsanız, getHostName kullanın. IP adresinin dize olarak temsil edilmesini istiyorsanız, getHostAddress() kullanın.

örnek:

InetAddress inet = InetAddress.getByName("193.125.22.1"); 
System.out.println(inet.toString()); 

fazla bilgi için bkz: http://www.java2s.com/Code/JavaAPI/javax.net/InetAddressgetByNameStringname.htm

+0

'a bakın: sadece adres dizisini istiyorsanız getHostAddress –