首页 > 代码库 > 创建 InetAddress 对象的四个静态方法
创建 InetAddress 对象的四个静态方法
package InetAddressClass;import java.net.InetAddress;import java.net.UnknownHostException;public class InetAddressStaticMethod { public static void main(String[] args) { InetAddressStaticMethod._getLocalHost(); InetAddressStaticMethod._getByName("www.csdn.net"); InetAddressStaticMethod._getAllByName("www.baidu.com"); InetAddressStaticMethod._getByAddress(); } public static void _getLocalHost() { InetAddress localAddress = null; try { localAddress = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(localAddress); System.out.println(localAddress.toString().split("/")[1]); } public static void _getByName(String hostname) { InetAddress address = null; try { address = InetAddress.getByName(hostname); } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(address); System.out.println(address.toString().split("/")[1]); } public static void _getAllByName(String hostname) { InetAddress[] addresses = null; try { addresses = InetAddress.getAllByName(hostname); } catch (UnknownHostException e) { e.printStackTrace(); } for(InetAddress address : addresses) { System.out.println(address); System.out.println(address.toString().split("/")[1]); } } public static void _getByAddress() { byte[] ip = new byte[] { (byte)141, (byte)146, 8, 66 }; InetAddress address1 = null; try { address1 = InetAddress.getByAddress(ip); } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(address1); InetAddress address2 = null; try { address2 = InetAddress.getByAddress("Oracle website", ip); } catch (UnknownHostException e) { e.printStackTrace(); } System.out.println(address2); } // java.net.InetAddress.java -- source code:// public String toString() {// String hostName = holder().getHostName();// return ((hostName != null) ? hostName : "")// + "/" + getHostAddress();// } // public static InetAddress getByName(String host)// throws UnknownHostException {// return InetAddress.getAllByName(host)[0];// } }
创建 InetAddress 对象的四个静态方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。