freemarker生成简单模板
生活随笔
收集整理的這篇文章主要介紹了
freemarker生成简单模板
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
簡單介紹
- conf目錄下有property和template兩個(gè)子目錄,分別放properties文件和ftl模板文件
- result目錄存放結(jié)果文件
- 在property和template中創(chuàng)建同名文件,但是文件后綴分別是properties和ftl
- global.properties是全局變量設(shè)置
- 備注一下?
freemarker的maven引用
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.31</version></dependency>代碼
?
import java.io.FileInputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Properties; import java.util.stream.Collectors;public class FreemarkerTest {public static final String TEMPLATE_PATH = "./conf/template";private static final String PROPERTIES_PATH = "./conf/property";private static final String RESULT_PATH = "./result";private static final String FTL_TYPE = ".ftl";private static final String PROPERTIES_TYPE = ".properties";private static final String GLOBAL_FILE = "./global.properties";public static void main(String[] args) {try {// 查看是否有全局的變量設(shè)置Path global = Paths.get(GLOBAL_FILE);Properties globalProperty = new Properties();if (Files.exists(global)) {globalProperty.load(new FileInputStream(GLOBAL_FILE));}// 創(chuàng)建結(jié)果目錄Path path = Paths.get(RESULT_PATH);boolean directory = Files.isDirectory(path);if (!directory) {Files.createDirectory(path);}Path ftlPath = Paths.get(TEMPLATE_PATH);List<Path> ftlCollect = Files.list(ftlPath).filter(a -> !Files.isDirectory(a) && a.getFileName().toString().endsWith(FTL_TYPE)).sorted().collect(Collectors.toList());Path proPath = Paths.get(PROPERTIES_PATH);List<Path> properties = Files.list(proPath).filter(a -> a.getFileName().toString().endsWith(PROPERTIES_TYPE)).sorted().collect(Collectors.toList());for (Path f : ftlCollect) {String fName = f.getFileName().toString();String realFtlName = fName.substring(0, fName.length() - FTL_TYPE.length());for (Path p : properties) {String pName = p.getFileName().toString();String propertyName = pName.substring(0, pName.length() - PROPERTIES_TYPE.length());if (realFtlName.equals(propertyName)) {FreemarkerUtil.createTemplate(fName, PROPERTIES_PATH + "/" + pName, RESULT_PATH + "/" + realFtlName + ".yaml", globalProperty);}}}} catch (Exception e) {e.printStackTrace();}} } import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;import java.io.*; import java.nio.charset.StandardCharsets; import java.util.Properties;public class FreemarkerUtil {private final static Configuration configuration = new Configuration(Configuration.VERSION_2_3_19);static {try {configuration.setDirectoryForTemplateLoading(new File(FreemarkerTest.TEMPLATE_PATH));} catch (IOException e) {e.printStackTrace();}}public static void createTemplate(String ftlName, String propertyName, String resultName, Properties global) throws IOException, TemplateException {Properties properties = new Properties();properties.putAll(global);properties.load(new FileInputStream(propertyName));// step4 加載模版文件Template template = configuration.getTemplate(ftlName);// step5 生成數(shù)據(jù)File docFile = new File(resultName);try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile), StandardCharsets.UTF_8))) {// step6 輸出文件template.process(properties, out);System.out.println("——————————————" + resultName + "模板文件創(chuàng)建成功!");} catch (Exception e) {e.printStackTrace();}} }總結(jié)
以上是生活随笔為你收集整理的freemarker生成简单模板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tomcat7.0安装及配置教程(win
- 下一篇: deepTools对ChIP-seq数据