版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JYL15732624861/article/details/81805521
@Test
public void testFreeMarker() throws Exception {
//1、创建一个模板文件
//2、创建一个Configuration对象
Configuration configuration = new Configuration(Configuration.getVersion());
//3、设置模板文件保存的目录
configuration.setDirectoryForTemplateLoading(new File("G:\\YiLiFang\\yilifang\\e3-parent\\e3-search-web\\src\\main\\webapp\\WEB-INF\\ftl"));
//4、模板文件的编码格式,一般就是utf-8
configuration.setDefaultEncoding("utf-8");
//5、加载一个模板文件,创建一个模板对象。
Template template = configuration.getTemplate("hello.ftl");
//6、创建一个数据集。可以是pojo也可以是map。推荐使用map
Map data = new HashMap<>();
data.put("hello1", "hello freemarker!");
//7、创建一个Writer对象,指定输出文件的路径及文件名。
Writer out = new FileWriter(new File("C:/Users/JYL/Desktop"));
//8、生成静态页面
template.process(data, out);
//9、关闭流
out.close();
}
java的FileWriter方法,执行抛出异常java.io.FileNotFoundException: C:\Users\JYL\Desktop (拒绝访问。)
文件中写入流,而不是目录,解决办法:将地址具体到文件。Writer out = new FileWriter(new File(“C:/Users/JYL/Desktop/hello.txt”));