package com.susu; import java.io.BufferedInputStream; 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 URLDemo2 { 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 res=null; String line=null; //读取数据 while((line=br.readLine())!=null){ res+=line; } br.close(); isr.close(); is.close(); System.out.println(res); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
作者:su20145104009 发表于2016/10/16 11:17:29 原文链接
阅读:62 评论:0 查看评论