URL类
URL 是Uniform Resource Location的缩写(统一资源定位符) 是internet上用来描述资源的字符串
javaSE API 中提供了URL类来封装
实例:
package netTest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; /** * URL 类构造方法有两种 new URL(String spec),new URL(URL context,String spec) * @author Administrator * */ public class TestURL { public static void main(String[] args) { try { URL url = new URL("http://www.xuedou.com"); System.out.println("主机名: "+url.getHost()); System.out.println("获取协议: "+url.getProtocol()); System.out.println("获取端口号: "+url.getPort()); System.out.println("获取文件: "+url.getFile()); System.out.println("======================================="); BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String strLine =""; while(null!=(strLine=br.readLine())){ System.out.println(strLine); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }