码迷,mamicode.com
首页 > 其他好文 > 详细

品鉴Velocity之 -- 文件读与写

时间:2014-09-29 15:29:11      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:des   io   os   ar   java   for   文件   c   on   

private static VelocityContext vc;

	static {
		vc = new VelocityContext();
	}

	public static void generateFile(String tempatePath, String destPath, Map<String, Object> attributes) {
		Template template = Velocity.getTemplate(tempatePath, "UTF-8");
		for (String key : attributes.keySet()) {
			vc.put(key, attributes.get(key));
		}
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8"));
			template.merge(vc, bw);
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
				}
			}
		}
	}

	public static void main(String[] args) {
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("key", "value");
		generateFile("路径/temp.vm", "路径/dest.html", attributes);
	}



品鉴Velocity之 -- 文件读与写

标签:des   io   os   ar   java   for   文件   c   on   

原文地址:http://my.oschina.net/miger/blog/323149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!