思路:

根据天气编码url获取2地天气;

运用StringSteamBuffer读取获取内容;

 1 package method;
 2 import java.io.BufferedReader;
 3 import java.io.InputStreamReader;
 4 import java.net.URL;
 5 /*
 6 功能:获取北京和张北的天气
 7  */
 8 public class WeatherReport {
 9         public static void main(String args[]) throws Exception{
10             try{
11                 //由网址创建URL对象
12                 URL urlBJ = new URL("http://www.weather.com.cn/data/cityinfo/101010100.html");
13                 URL urlZB = new URL("http://www.weather.com.cn/data/cityinfo/101090303.html");
14                 //“UTF-8”这是为了防止乱码
15                 InputStreamReader isReaderBJ =  new InputStreamReader(urlBJ.openStream(),"UTF-8");
16                 InputStreamReader isReaderZB = new InputStreamReader(urlZB.openStream(),"UTF-8");
17                 //采用缓冲式读入
18                 BufferedReader br_BJ = new BufferedReader(isReaderBJ);
19                 BufferedReader br_ZB = new BufferedReader(isReaderZB);
20                 String str_BJ;
21                 String str_ZB;
22 
23                 System.out.println("北京天气:");
24                 while((str_BJ = br_BJ.readLine()) != null){
25                     System.out.println(str_BJ);//输出
26                 }
27 
28                 System.out.println("张北天气:");
29                 while((str_ZB = br_ZB.readLine())!= null){
30                     System.out.println(str_ZB);
31                 }
32 
33                 //网上资源使用结束后,数据流及时关闭
34                 br_BJ.close();
35                 br_ZB.close();
36                 isReaderBJ.close();
37                 isReaderZB.close();
38             }
39             catch(Exception exp){
40                 System.out.println(exp);
41             }
42 
43         }
44 }
View Code