读书人

freemarker的应用

发布时间: 2012-09-06 10:37:01 作者: rapoo

freemarker的使用
package com.example.test;
import freemarker.template.*;

import java.util.*;
import java.io.*;

public class ThreadTest {

/**
* @param s
* @throws IOException
* @throws TemplateException
*/
public static void main(String[] s) throws IOException, TemplateException{
Configuration cfg = new Configuration();

cfg.setDirectoryForTemplateLoading(new File("freemaker"));

cfg.setObjectWrapper(new DefaultObjectWrapper());

Template temp = cfg.getTemplate("freemaker.java");

Map root = new HashMap();

List<Field> fieldList = new ArrayList<Field>();
Field field1 = new Field();
field1.setName("1");
fieldList.add(field1);
Field field2 = new Field();
field1.setName("2");
fieldList.add(field2);
Field field3 = new Field();
field1.setName("3");
fieldList.add(field3);

root.put("user", "qinyouyitest");
root.put("fieldList", fieldList);
Writer out = new OutputStreamWriter(System.out);
//文件自动生成技术

temp.process(root, out);
out.flush();



FileOutputStream fileoutput = new FileOutputStream(new File("src/com/example/test/Test.java"));
Writer writer = new OutputStreamWriter(fileoutput,"UTF-8");
writer.write("int");
writer.flush();
fileoutput.flush();

}

}

读书人网 >编程

热点推荐