Velocity 10th application examples-sample code output to a file. For more information, see velocity 10th application examples-output to a file
// 2 Create a Context objectVelocityContext context = newVelocityContext (); // 3 Add you data object to this contextcontext. put ("title", "UnionPay electronics"); context. put ("body", "This is the content"); // 4 Choose a templateTemplate template = Velocity. getTemplate ("file. vm "); // create a File saveFile = newFile (" G: \ workspace \ zjq \ velocity \ WebRoot \ page \ test.html "); // obtain its parent class file. if it does not exist, create if (! SaveFile. getParentFile (). exists () {saveFile. getParentFile (). mkdirs () ;}// create a file output stream FileOutputStream outStream = newFileOutputStream (saveFile); // A Writer is required for template integration, therefore, create a WriterOutputStreamWriter writer = newOutputStreamWriter (outStream); // create a buffer stream BufferedWriter bufferWriter = newBufferedWriter (writer); // 5 Merge the template and you data toproduce the outputtemplate. merge (context, bufferWriter); bufferWriter. flush (); // force refresh outStream. close (); bufferWriter. close ();
File. vm
$title$body
The above is velocity. the content output to the file. For more information, see The PHP Chinese website (www.php1.cn )!