天气预报程序-抓取和文件读写

本文介绍了如何编写一个天气预报程序,涵盖了从网上抓取天气信息并进行解析,再到将数据存储到文件中进行读写的完整过程。通过使用字符串处理、日期格式化和缓冲区操作,实现数据的有效管理和持久化。文章还讨论了处理null值和输入验证的重要性。
摘要由CSDN通过智能技术生成
package com.zzk.cn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

/**
 * 
 * @author zhuzhengke
 * @version 1.0.0
 * 
 */
class ReadProperties {

	public String getProperties(String key) {
		Properties prop = new Properties();
		InputStream inputStream = this.getClass().getResourceAsStream(
				"/weather_code.properties");
		try {
			prop.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭
			if (null != inputStream) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return prop.getProperty(key);
	}

}

public class GetWeather {

	/* 主函数 */
	public static void main(String[] args) {
		//json();
		//获取文件
		
		Properties prop = new Properties();
		InputStream inputStream = new GetWeather().getClass()
				.getResourceAsStream("/weather_code.properties");
		try {
			prop.load(inputStream);
			System.out.println(prop.getProperty("101020100"));
			Enumeration<?> en = prop.propertyNames();
			while (en.hasMoreElements()) {
				String obj = (String) en.nextElement();
				System.out.println(obj);
				
				//抓取网页信息
				getInfo(obj);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭资源
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	
	
	/**
	 * Json解析
	 */
//	public static void json() {
//
//		// 以下为JSON格式抓取实例
//		/**
//		 * {"weatherinfo":{"city":"北京","city_en":"beijing","date_y":
//		 * "2011年11月22日","date":"辛卯年","week":"星期二","fchh":"11",
//		 * "cityid":"101010100"
//		 * ,"temp1":"12℃~-2℃","temp2":"7℃~-3℃","temp3":"8℃~-1℃"
//		 * ,"temp4":"12℃~3℃","temp5":"12℃~4℃",
//		 * "temp6":"11℃~4℃","tempF1":"53.6℉~28.4℉"
//		 * ,"tempF2":"44.6℉~26.6℉","tempF3"
//		 * :"46.4℉~30.2℉","tempF4":"53.6℉~37.4℉",
//		 * "tempF5":"53.6℉~39.2℉","tempF6"
//		 * :"51.8℉~39.2℉","weather1":"多云转晴","weather2"
//		 * :"晴","weather3":"晴","weather4":"晴转多云",
//		 * "weather5":"多云转晴","weather6":"阴"
//		 * ,"img1":"1","img2":"0","img3":"0","img4"
//		 * :"99","img5":"0","img6":"99","img7":"0","img8":"1",
//		 * "img9":"1","img10"
//		 * :"0","img11":"2","img12":"99","img_single":"1","img_title1"
//		 * :"多云","img_title2":"晴","img_title3":"晴",
//		 * "img_title4":"晴","img_title5"
//		 * :"晴","img_title6":"晴","img_title7":"晴","img_title8"
//		 * :"多云","img_title9":"多云","img_title10":"晴",
//		 * "img_title11":"阴","img_title12"
//		 * :"阴","img_title_single":"多云","wind1":"北风4-5级"
//		 * ,"wind2":"微风","wind3":"微风","wind4":"微风","wind5":
//		 * "微风","wind6":"微风","fx1"
//		 * :"北风","fx2":"北风","fl1":"4-5级","fl2":"小于3级","fl3"
//		 * :"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
//		 * "index":"凉","index_d":
//		 * "天气凉,建议着厚外套加毛衣等春秋服装。体弱者宜着大衣、呢外套。因昼夜温差较大,注意增减衣服。"
//		 * ,"index48":"冷","index48_d":
//		 * "天气冷,建议着棉衣、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣或冬大衣。","index_uv"
//		 * :"弱","index48_uv":"中等","index_xc":"较适宜","index_tr":"很适宜",
//		 * "index_co":"较舒适"
//		 * ,"st1":"11","st2":"-4","st3":"4","st4":"0","st5":"8","st6"
//		 * :"0","index_cl":"较不宜","index_ls":"基本适宜","index_ag": "极不易发"}}
//		 */
//		JSONObject jsonob = JSONObject.fromObject((JSONObject.fromObject(info)
//				.getString("weatherinfo")));
//
//		String city = jsonob.getString("city");// 城市
//		System.out.println(city);
//
//		String date_y = jsonob.getString("date_y");// 时间
//		System.out.println(date_y);
//
//		String date = jsonob.getString("date");// 农历年
//		System.out.println(date);
//
//		String week = jsonob.getString("week");// 星期
//		System.out.println(week);
//
//		String cityid = jsonob.getString("cityid");// 城市号
//		
//		System.out.println(cityid);
//
//		String temp1 = jsonob.getString("temp1");// 以下是六天内摄氏温度
//		System.out.println(temp1);
//
//		String temp2 = jsonob.getString("temp2");
//		System.out.println(temp2);
//
//		String temp3 = jsonob.getString("temp3");
//		System.out.println(temp3);
//
//		String temp4 = jsonob.getString("temp4");
//		System.out.println(temp4);
//
//		String temp5 = jsonob.getString("temp5");
//		System.out.println(temp5);
//
//		String temp6 = jsonob.getString("temp6");
//		System.out.println(temp6);
//
//		String tempF1 = jsonob.getString("tempF1");// 以下是六天内华氏温度
//		System.out.println(tempF1);
//
//		String tempF2 = jsonob.getString("tempF2");
//		System.out.println(tempF2);
//
//		String tempF3 = jsonob.getString("tempF3");
//		System.out.println(tempF3);
//
//		String tempF4 = jsonob.getString("tempF4");
//		System.out.println(tempF4);
//
//		String tempF5 = jsonob.getString("tempF5");
//		System.out.println(tempF5);
//
//		String tempF6 = jsonob.getString("tempF6");
//		System.out.println(tempF6);
//
//		String weather1 = jsonob.getString("weather1");// 以下是六天天气
//		System.out.println(weather1);
//
//		String weather2 = jsonob.getString("weather2");
//		System.out.println(weather2);
//
//		String weather3 = jsonob.getString("weather3");
//		System.out.println(weather3);
//
//		String weather4 = jsonob.getString("weather4");
//		System.out.println(weather4);
//
//		String weather5 = jsonob.getString("weather5");
//		System.out.println(weather5);
//
//		String weather6 = jsonob.getString("weather6");
//		System.out.println(weather6);
//
//		String wind1 = jsonob.getString("wind1");// 以下六天为风力
//		System.out.println(wind1);
//
//		String wind2 = jsonob.getString("wind2");
//		System.out.println(wind2);
//
//		String wind3 = jsonob.getString("wind3");
//		System.out.println(wind3);
//
//		String wind4 = jsonob.getString("wind4");
//		System.out.println(wind4);
//
//		String wind5 = jsonob.getString("wind5");
//		System.out.println(wind5);
//
//		String wind6 = jsonob.getString("wind6");
//		System.out.println(wind6);
//
//		String fl1 = jsonob.getString("fl1");// 以下为六天风级
//		System.out.println(fl1);
//
//		String fl2 = jsonob.getString("fl2");
//		System.out.println(fl2);
//
//		String fl3 = jsonob.getString("fl3");
//		System.out.println(fl3);
//
//		String fl4 = jsonob.getString("fl4");
//		System.out.println(fl4);
//
//		String fl5 = jsonob.getString("fl5");
//		System.out.println(fl5);
//
//		String fl6 = jsonob.getString("fl6");
//		System.out.println(fl6);
//
//		String index_d = jsonob.getString("index_d");// 当日穿衣指数
//		System.out.println(index_d);
//
//		System.out.println();
//		try{
//			Class.forName("com.mysql.jdbc.Driver");
//		
//    	//new oracle.jdbc.driver.OracleDriver;
//         conn=DriverManager.getConnection("jdbc:mysql://10.1.101.223:3306/weather", "appuser", "opzoon123!");
//       
//    	pstmt=conn.prepareStatement("insert into weather_info(city_id,weather_date,weather_year,weather_week,weather_temp,weather_winddirection,weather_windpower,weather_description) values(?,?,?,?,?,?,?,?)");
//    	
//    	pstmt.setInt(1,Integer.parseInt(cityid));
//    	pstmt.setString(2,date_y);
//    	pstmt.setString(3,date);
//    	pstmt.setString(4,week);
//    	pstmt.setString(5,temp1);
//    	pstmt.setString(6,wind1);
//    	pstmt.setString(7, fl1);
//    	pstmt.setString(8,index_d);
//    	pstmt.executeUpdate();
//		}catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		}catch (SQLException e) {
//			e.printStackTrace();
//		}finally {
//			try {
//				pstmt.close();
//			} catch (SQLException e1) {
//				e1.printStackTrace();
//			}
//			try {
//				conn.close();
//			} catch (SQLException e) {
//				e.printStackTrace();
//			}
//		}
//
//		// System.out.println("以下仅仅是一个Demo");
//		// 解析的数据格式2:{"classroom":"0801","peoples":[{"field1":"name1","field2":"age1"},{"field0":"name2","field2":"age2说"}]}
//		// info =
//		// "{\"classroom\":\"111\",\"peoples\":[{\"field1\":\"zzk1\",\"field2\":\"age1\"},{\"field1\":\"zzk2\",\"field2\":\"age2\"}]}";
//		//
//		// jsonob = JSONObject.fromObject(info);
//		//
//		// String classname = jsonob.getString("classroom");
//		// System.out.println(classname);
//		//
//		// JSONArray jsons = jsonob.getJSONArray("peoples");
//		// int jsonLength = jsons.size();
//		//
//		// // 对json数组进行循环
//		// for (int i = 0; i < jsonLength; i++) {
//		// JSONObject tempJson = JSONObject.fromObject(jsons.get(i));
//		//
//		// String name = StringEscapeUtils.escapeSql(tempJson
//		// .getString("field1"));
//		// String age = StringEscapeUtils.escapeSql(tempJson
//		// .getString("field2"));
//		//
//		// System.out.println(name + "-" + age);
//		// }
//	}

	/**
	 * 获取网页信息
	 */
	public static void getInfo(String id) {
		String path = "http://m.weather.com.cn/data/" + id + ".html";
		URL url;
		String inputline = "";
		InputStream input = null;
		InputStreamReader reader = null;
		BufferedReader buffer = null;
		try {
			url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setReadTimeout(10 * 1000);
			conn.setRequestMethod("GET");

			input = conn.getInputStream();
			reader = new InputStreamReader(input,"utf8");
			buffer = new BufferedReader(reader);
			
			while ((inputline = buffer.readLine()) != null) {
				System.out.println(inputline);
			}
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != buffer) {
					buffer.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			buffer = null;
			
			try {
				if (null != reader) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			reader = null;
			
			try {
				if (null != input) {
					input.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			input = null;
			
		}
		
	}
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值