`
danandyu8013
  • 浏览: 39215 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

历史记录存文件

    博客分类:
  • JAVA
阅读更多

 

public void backupData() {

Calendar c = Calendar.getInstance();

String day=MainConfig.SAVE_MONTH.trim();

c.add(Calendar.DATE, -Integer.parseInt(day));//

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

String s = df.format(c.getTime());// 配置文件中的前N天

 

/*Calendar c1 = Calendar.getInstance();

int da=Integer.parseInt(day)+1;

c1.add(Calendar.DATE, -(da));//

String d = df.format(c1.getTime());//配置文件中的前N+1天

*/

OutputStreamWriter writer = null;

List<Weather> list = weatherDaoBiz.GetWeatherInfoListAll(s);// 获取天气预报数据

try {

if (list != null) {

// ////////////创建文件夹/////////

String strname = MainConfig.WEATHER_BACK_PATH;

if (!new File(strname).exists()) {

new File(strname).mkdir();

}

strname = MainConfig.WEATHER_BACK_PATH + File.separator

+ DateUtil.getYear();

if (!new File(strname).exists()) {

new File(strname).mkdir();

}

strname = MainConfig.WEATHER_BACK_PATH + File.separator

+ DateUtil.getYear() + File.separator

+ DateUtil.getMonthDay();

if (!new File(strname).exists()) {

new File(strname).mkdir();

}

// //////////////////////////////////

long time = new Date().getTime();// 获取格林时间,用来做文件名

File file = new File(strname + File.separator + "Weather" + time + ".xml");// 声明文件

JAXBContext content = JAXBContext.newInstance(WeatherXml.class);

Marshaller mar = content.createMarshaller();

mar.setProperty(Marshaller.JAXB_ENCODING, "utf-8");

mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);

WeatherXml p = new WeatherXml();

p.setList(list);

writer = new OutputStreamWriter(new FileOutputStream(file),

"utf-8");

mar.marshal(p, writer);

logger.info("保存"+s+"天的天气预报到"+strname);

writer.close();// 关闭

//////////////删除s天的天气预记录///////////////////

logger.info("删除"+s+"天前天气预");

weatherDaoBiz.deleteOle(s);  

logger.info("删除完成");

}

} catch (Exception e) {

logger.warn("转换XML出异常" + e);

writer=null;

}

}

 

 

配置文件:

 

 

#删除前N天的数据

DELETE_WEATHER_DAY = 1

#存文件的路径

WEATHER_BACK_PATH =d:/weather

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics