博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
freemarker-按格式输出文件
阅读量:7079 次
发布时间:2019-06-28

本文共 1776 字,大约阅读时间需要 5 分钟。

使用步骤如下:

pospCheck.ftl模板文件如下:

<#list resultList as dto>

${${dto.oWebSeqNum?default("")},${dto.transDate?default("")},${dto.diffType?default("")},${dto.frontStatus?default("")}
</#list>

 

2.public void buildTestFile(String date) {

List<CheckFeeDto> fileData = CheckDao.getPctData(date);
Map map = new HashMap();
map.put("resultList", fileData);
String charset = "GBK";
// 模板文件为FTL
Writer writer = null;
try {
String saveLocalPath = FilePath + date.substring(0, 6) + "/"
+ date.substring(6, 8) + "/";
File checkFileDir = new File(saveLocalPath);
if (!checkFileDir.exists())
{
checkFileDir.mkdirs();
}
String basePath = this.getClass().getResource("/ftl_templates").getPath();
File file=new File(basePath+ "/withdrawFee.ftl");
Configuration conf = Configuration.getDefaultConfiguration();
conf.setDirectoryForTemplateLoading(new File(basePath));
conf.setEncoding(Locale.CHINA, "utf-8");
conf.setOutputEncoding(charset);
Template template = conf.getTemplate(file.getName());
writer = new CharArrayWriter();
template.process(map, writer);
writer.flush();
String filename =saveLocalPath+"FF_" + date + ".txt";
File outfile = new File(filename);
OutputStream os = new FileOutputStream(outfile);
FileUtil.save(os, writer.toString().getBytes(charset));
os.flush();
os.close();
writer.close();
} catch (Exception e) {
return null;
} finally {
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

 

3.FileUtil 类中的save 方法

/**

* 保存字节到输出流中.
* @param os 输出流
* @param bytes 字节流
* @throws IOException
* 调用完后由调用者应该关闭os输出流.
*/
public static void save(final OutputStream os, final byte[] bytes)
throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(os);
bos.write(bytes);
bos.flush();
}

 

转载于:https://www.cnblogs.com/tlyben/p/5141509.html

你可能感兴趣的文章
PyYAML序列化yaml文件数据
查看>>
Radmin远程连接TMG
查看>>
CCNA 学习笔记(三)--路由选择协议(静态路由协议)
查看>>
python 学习笔记(4)-转载
查看>>
python实例pyspark以及python中文显示
查看>>
一个典型核心网络故障分析
查看>>
获取lamp编译参数
查看>>
Shell理论学习(一)
查看>>
phpcms开发之模板语法规则
查看>>
CST UTC
查看>>
因为看见,所以发现:QBotVariant谢绝落幕
查看>>
我的友情链接
查看>>
让Apache支持shtml实现include文件解析的配置方法
查看>>
软件测试学习:检查产品说明书
查看>>
linux 防火墙
查看>>
mysql事务提交模式
查看>>
word search 此题若会,所有dfs矩阵全会
查看>>
ASP.NET Cache的一些总结2
查看>>
JAVA中易出错的小问题(二)
查看>>
asp.net 用正则表达式过滤内容中的电话,qq,email
查看>>