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><html><head><metacharset="UTF-8"><title>$title</title></head><body>$body</body></html>
以上就是velocity,輸出到檔案的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!