private void exportByFreemarker() {
Configuration cfg = new Configuration();
// try {
// cfg.setDirectoryForTemplateLoading(new File("F:/ftl"));
// } catch (IOException e) {
// System.out.println("template not found!");
// e.printStackTrace();
// }
cfg.setClassForTemplateLoading(this.getClass(), "ftl");
cfg.setObjectWrapper(new DefaultObjectWrapper());
Map root = new HashMap();
root.put("time", new Date().toString());
try {
Template templateHtml = cfg.getTemplate("template filename");
// Writer out = new OutputStreamWriter(System.out);
Writer outHtml = new FileWriter(new File("new file path"));
templateHtml.process(root, outHtml);
outHtml.flush();
outHtml.close();
} catch (IOException e) {
System.out.println("error to read template file");
e.printStackTrace();
} catch (TemplateException e) {
System.out.println("creat file error");
}
System.out.println("[info] Done! ");
}
一个freemarker生成文件的简单例子
最新推荐文章于 2020-06-30 00:32:20 发布