package com.chl.Java_freemarker;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FilterOutputStream;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.HashMap;
    import java.util.Map;

    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;

    /*
    Hello world!

    /
    public class App
    {
    //声明模板路径 test.ftl
    private static final String TEMPLATE_PATH=”src/main/java/com/chl/template”;

    //生成java的路径
    private static final String JAVA_PATH=”src/main/java/com/chl/model”;
    //—————————————————————————————————————
    public static void main( String[] args )
    {
    BufferedWriter bw=null;
    //1、freemarker的配置对象
    Configuration cf=new Configuration();
    try
    { //2、加载模板路径
    cf.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));

    //3、构建数据
    Map maps=new HashMap();
    maps.put(“classpath”, “com.chl.model”);
    maps.put(“className”, “Stu”);
    maps.put(“name”, “stuName”);

    Template template = cf.getTemplate(“test.ftl”);

    //4、生产java文件
    File f=new File(JAVA_PATH+”\“+”Stu.java”);
    if(!f.getParentFile().exists())
    {
    f.getParentFile().mkdirs();
    }
    bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
    //bw产生了一个写入文件的输入流,maps里面的数据嵌入模板,进而写入目标文件
    template.process(maps, bw);

    } catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (TemplateException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally {
    if (null!=bw)
    {
    try
    {
    bw.close();
    } catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    }