velocity第10个应用例子---输出到文件
2015-07-22 15:29
330 查看
//2 Create a Context object
VelocityContext context = newVelocityContext();
//3 Add you data object to this context
context.put("title", "银联电子");
context.put("body", "这是内容");
//4 Choose a template
Template template =Velocity.getTemplate("file.vm");
//创建文件
File saveFile = newFile("G:\\workspace\\zjq\\velocity\\WebRoot\\page\\test.html");
//获得它的父类文件,如果不存在,就创建
if(!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
//创建文件输出流
FileOutputStream outStream = newFileOutputStream(saveFile);
//因为模板整合的时候,需要提供一个Writer,所以创建一个Writer
OutputStreamWriter writer = newOutputStreamWriter(outStream);
//创建一个缓冲流
BufferedWriter bufferWriter = newBufferedWriter(writer);
//5 Merge the template and you data toproduce the output
template.merge(context, bufferWriter);
bufferWriter.flush();//强制刷新
outStream.close();
bufferWriter.close();
file.vm
<!DOCTYPE
html>
<html>
<head>
<meta
charset="UTF-8">
<title>$title</title>
</head>
<body>
$body
</body>
</html>
//2 Create a Context object
VelocityContext context = newVelocityContext();
//3 Add you data object to this context
context.put("title", "银联电子");
context.put("body", "这是内容");
//4 Choose a template
Template template =Velocity.getTemplate("file.vm");
//创建文件
File saveFile = newFile("G:\\workspace\\zjq\\velocity\\WebRoot\\page\\test.html");
//获得它的父类文件,如果不存在,就创建
if(!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
//创建文件输出流
FileOutputStream outStream = newFileOutputStream(saveFile);
//因为模板整合的时候,需要提供一个Writer,所以创建一个Writer
OutputStreamWriter writer = newOutputStreamWriter(outStream);
//创建一个缓冲流
BufferedWriter bufferWriter = newBufferedWriter(writer);
//5 Merge the template and you data toproduce the output
template.merge(context, bufferWriter);
bufferWriter.flush();//强制刷新
outStream.close();
bufferWriter.close();
file.vm
<!DOCTYPE
html>
<html>
<head>
<meta
charset="UTF-8">
<title>$title</title>
</head>
<body>
$body
</body>
</html>
相关文章推荐
- 【算法学习笔记】82.素数生成 数据范围利用 SJTU OJ 1020 分解质因数
- B - The Suspects -poj 1611
- 【连载】Maven系列(三) 进阶
- iOS中assign、copy 、retain等关键字的含义
- Sqlyog11注册码\下载地址
- hdu 1829 A Bug's Life(分组并查集)
- 多校I 1001
- 【BLE】CC2541之解析协议栈的晶振初始化过程
- POJ1068 Parencodings
- LoadLibrary失败
- crm创建报告补充导航
- 平衡的括号(栈)
- C#+Socket客户端断线重连的解决办法
- CodeForces 55D Beautiful numbers (数位DP)
- spring事物管理
- velocity第9个应用例子 ---格式化日期
- OS中atomic的实现解析
- Hybirs 使用impex删除一个表的所有内容
- “工业4.0”
- 2012年5月SAT香港真题解析