依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>2.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
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<String, String> 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());
}