Velocity 10th application example --- output to file

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.