velocity第10个应用例子—输出到文件
//2 Create a Context objectVelocityContext context = newVelocityContext();//3 Add you data object to this contextcontext.put("title", "银联电子");context.put("body", "这是内容"); //4 Choose a templateTemplate 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,所以创建一个WriterOutputStreamWriter writer = newOutputStreamWriter(outStream);//创建一个缓冲流BufferedWriter bufferWriter = newBufferedWriter(writer);//5 Merge the template and you data toproduce the outputtemplate.merge(context, bufferWriter);bufferWriter.flush();//强制刷新outStream.close();bufferWriter.close();
file.vm
<!DOCTYPEhtml>&<mark>6来源gaodaimacom搞#^代%!码网</mark><strong>搞gaodaima代码</strong>lt;html><head><metacharset="UTF-8"><title>$title</title></head><body>$body</body></html>
以上就是velocity,输出到文件的内容,更多相关内容请关注搞代码(www.gaodaima.com)!