java inetAddress类,URL类

来源:互联网 发布:中信淘宝信用卡额度 编辑:程序博客网 时间:2024/04/29 17:16

InetAddress类

<span style="font-family:Microsoft YaHei;font-size:18px;">import java.net.InetAddress;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Arrays;public class Test {public static void main(String[] args) throws UnknownHostException {// TODO Auto-generated method stub//获取本机的InetAddressInetAddress address=InetAddress.getLocalHost();//获取本机名称System.out.println("计算机名:"+address.getHostName());//获取本机ip地址System.out.println("计算机地址:"+address.getHostAddress());//获取字节数组形式的地址byte[]bytes=address.getAddress();System.out.println("计算机地址字节形式:"+Arrays.toString(bytes));//输出本机的 计算机名/ip地址System.out.println("直接输出InetAddress实例:"+address);System.out.println();//根据主机名获取InetAddress实例//InetAddress address2=InetAddress.getByName("sujm_PC");///出错,不知道为什么getHostName在这里是ip地址。。。InetAddress address2=InetAddress.getByName("219.216.85.246");System.out.println("计算机名:"+address2.getHostName());System.out.println("计算机ip地址:"+address2.getHostAddress());}}</span>
URL类的基本方法

<span style="font-family:Microsoft YaHei;font-size:18px;">package com.sjm;import java.net.MalformedURLException;import java.net.URL;import java.text.RuleBasedCollator;public class Test2 {<span style="white-space:pre"></span>public static void main(String[] args) {<span style="white-space:pre"></span>// TODO Auto-generated method stub<span style="white-space:pre"></span>try {<span style="white-space:pre"></span>//创建一个url实例<span style="white-space:pre"></span>URL imooc=new URL("http://www.imooc.com");<span style="white-space:pre"></span>//问号后表示参数,#后表示锚点<span style="white-space:pre"></span>URL url=new URL(imooc,"/index.html?username=tom#test");<span style="white-space:pre"></span>//获取url的协议信息<span style="white-space:pre"></span>System.out.println("协议:"+url.getProtocol());<span style="white-space:pre"></span>//获取url的主机<span style="white-space:pre"></span>System.out.println("主机:"+url.getHost());<span style="white-space:pre"></span>//获取url的端口号,默认端口号是80,如果未指定端口,根据协议使用默认端口号,此时getPort()的返回值为-1<span style="white-space:pre"></span>System.out.println("端口号:"+url.getPort());<span style="white-space:pre"></span>//获取url中文件的路径<span style="white-space:pre"></span>System.out.println("文件路径:"+url.getPath());<span style="white-space:pre"></span>System.out.println("文件名:"+url.getFile()); //文件路径+参数<span style="white-space:pre"></span>System.out.println("相对路径:"+url.getRef());//锚点后的内容<span style="white-space:pre"></span>System.out.println("查询字符串:"+url.getQuery());//参数<span style="white-space:pre"></span>} catch (MalformedURLException e) {<span style="white-space:pre"></span>// TODO Auto-generated catch block<span style="white-space:pre"></span>e.printStackTrace();<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}</span>
URL类的读数据


<span style="font-family:Microsoft YaHei;font-size:18px;">import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;public class Test03 {public static void main(String[]args) {try {//创建url实例URL url=new URL("http://www.baidu.com");//通过url.openStream()方法,获取url对象所表示的资源的字节输入流 InputStream is=url.openStream();//将字节输入流转换为字符输入流InputStreamReader isr=new InputStreamReader(is,"utf-8");//为字符输入流添加缓冲BufferedReader br=new BufferedReader(isr);String data=br.readLine();while(data!=null){System.out.println(data);data=br.readLine();}br.close();is.close();} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}</span></span>




0 0
原创粉丝点击