java中如何获取IP及电脑名

main方法,其实仅仅就对代码挺少的;具体代码如下所示,由于InetAdress.getLocalHost()方法需要抛出一个异常所以就采用try catch结构了:

public class getiptext {



public static void main(String[] args) {

   try{

            InetAddress myip= InetAddress.getLocalHost();

            System.out.println("你的IP地址是:"+myip.getHostAddress());

            System.out.println("主机名为:"+myip.getHostName()+"。");

      }catch(Exception e){

           e.printStackTrace();

     }

     }

}

 

你可能感兴趣的:(工具类)