http://wenku.baidu.com/view/b401add728ea81c758f57882.html?re=view
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.edu; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.PrintWriter; 6 import java.io.Writer; 7 8 import org.apache.velocity.Template; 9 import org.apache.velocity.VelocityContext; 10 import org.apache.velocity.app.Velocity; 11 import org.apache.velocity.app.VelocityEngine; 12 //http://wenku.baidu.com/view/b401add728ea81c758f57882.html?re=view 13 public class VelocityTest { 14 15 public static void main(String[] args) throws Exception{ 16 VelocityEngine ve=new VelocityEngine(); 17 String path="D:/workspace/velocityPro/HelloVelocity/src/cn/edu/"; 18 ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); 19 20 ve.setProperty(Velocity.INPUT_ENCODING, "GBK"); 21 ve.setProperty(Velocity.OUTPUT_ENCODING, "GBK"); 22 23 ve.init(); 24 25 Template template =ve.getTemplate("hello.html"); 26 27 VelocityContext root=new VelocityContext(); 28 29 root.put("name", "valueadfasdf"); 30 String outpath="e:/helloworld.html"; 31 Writer mywriter= new PrintWriter(new FileOutputStream(new File(outpath))); 32 template.merge(root, mywriter); 33 mywriter.flush(); 34 35 } 36 37 }