首页 > 代码库 > 品鉴Velocity之 -- 文件读与写

品鉴Velocity之 -- 文件读与写

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之 -- 文件读与写