依赖:
org.springframework.boot spring-boot-starter-freemarker2.3.2.RELEASE
cn.hutool hutool-all5.7.22
public static void main(String[] args) throws IOException, TemplateException {
//设置模板
Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
configuration.setTemplateLoader(new StringTemplateLoader());
configuration.setDefaultEncoding("utf-8");
//准备数据
HashMap map = new HashMap<>();
map.put("name", "张三丰");
//准备模板
String model = "你好,${name}";
//构建
Template template = new Template("You", model, configuration);
//输出
StringWriter writer = new StringWriter();
template.process(map, writer);
//打印
System.out.println(writer.toString());
//创建文件、如果多层路径不存在则创建
File touch = FileUtil.touch("D:/aaa/a.txt");
FileWriter w = new FileWriter(touch);
//写入文件
w.write(writer.toString());
}
暑期编程PK赛
得CSDN机械键盘等精美礼品!


