zoukankan      html  css  js  c++  java
  • 用Main方法调用freemarker生成文件

    MyGenerator.java

    package com.comp.common;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import freemarker.template.Configuration;
    import freemarker.template.DefaultObjectWrapper;
    import freemarker.template.Template;
    
    public class MyGenerator {
    
        public static void main(String[] args) {
            try {
                Map root = new HashMap();
                root.put("str", "hello world!");
                List data = new ArrayList();
                data.add("11");
                data.add("12");
                data.add("13");
                root.put("data", data);
                
                Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
                cfg.setDirectoryForTemplateLoading(new File("D:/web/template/"));
                cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
                Template temp = cfg.getTemplate("demo.ftl");
                String fileName = "demo.htm";
                File file = new File("D:/web/template/" + fileName);
                FileWriter fw = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(fw);
                temp.process(root, bw);
                bw.flush();
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
    }

    模板文件D:/web/template/demo.ftl

    ${str}
    <#list data as row>
    ${row}
    </#list>

    生成的文件D:/web/template/demo.html

    hello world!
    11
    12
    13
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/modou/p/6354230.html
Copyright © 2011-2022 走看看