之前做rcp开发的时候用到freemaker技术,这里记录下,留个纪念。
.project文件内又一个变量${pro_name.full}
private static Configuration cfg = new Configuration();
public static void generateFile() throws IOException{
Map <String, String> map = new HashMap<String, String>();
map.put("pro_name.full", "项目名");
cfg.setDefaultEncoding("UTF-8");
cfg.setDirectoryForTemplateLoading(new File("D:\\"));
Template template = cfg.getTemplate(".project");
String tofile = "D:\\.project";
FileOutputStream fos = new FileOutputStream(tofile);
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
try {
template.process(map, osw);
} catch (TemplateException e) {
} finally {
osw.close();
}
}
运行后会发现原来的变量,变成了项目名。
freemaker是一个非常不错的框架。