main:
Configuration cfg = new Configuration();
// 加载freemarker模板文件
cfg.setDirectoryForTemplateLoading(new File("F:\\sunline\\flow6\\demo\\src\\freemarker"));
以上为初始化模板环境
// 定义并设置数据
Map<String, String> data = new HashMap<String, String>();
data.put("persion", "小吴");
// 获取指定模板文件
Template template = cfg.getTemplate("test.ftl");
// 定义输入文件,默认生成在工程根目录
Writer out = new OutputStreamWriter(new FileOutputStream("test.txt"),
"GBK");
// 最后开始生成
template.process(data, out);
System.out.println("successful");
这里存放 模板文件
F:\\sunline\\flow6\\demo\\src\\freemarker
test.ftl
The first test: ${persion}
运行之后就会生成test.txt文件。
文件内容为:
The first test: 小吴
模板语言。