当前位置:Gxlcms > PHP教程 > velocity第10个应用例子---输出到文件

velocity第10个应用例子---输出到文件

时间:2021-07-01 10:21:17 帮助过:23人阅读

velocity第10个应用例子---输出到文件

  1. //2 Create a Context object
  2. VelocityContext context = newVelocityContext();
  3. //3 Add you data object to this context
  4. context.put("title", "银联电子");
  5. context.put("body", "这是内容");
  6. //4 Choose a template
  7. Template template =Velocity.getTemplate("file.vm");
  8. //创建文件
  9. File saveFile = newFile("G:\\workspace\\zjq\\velocity\\WebRoot\\page\\test.html");
  10. //获得它的父类文件,如果不存在,就创建
  11. if(!saveFile.getParentFile().exists()) {
  12. saveFile.getParentFile().mkdirs();
  13. }
  14. //创建文件
输出流 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

  1. <!DOCTYPE
  2. html>
  3. <html>
  4. <head>
  5. <meta
  6. charset="UTF-8">
  7. <title>$title</title>
  8. </head>
  9. <body>
  10. $body
  11. </body>
  12. </html>

以上就是velocity,输出到文件的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行