研究hibernatesynchronizer的源碼,看到他將velocity模板和編譯的類一起打包在jar包中,在獲得模板時使用
Xobject.class.getClassLoader().getResourceAsStream("/templates/xx.vm")獲得流,然后再將轉變成字符串
public static String getStringFromStream(InputStream is) throws IOException {
??????? if (null == is)
??????????? return null;
??????? try {
??????????? InputStreamReader reader = new InputStreamReader(is);
??????????? char[] buffer = new char[1024];
??????????? StringWriter writer = new StringWriter();
??????????? int bytes_read;
??????????? while ((bytes_read = reader.read(buffer)) != -1) {
??????????????? writer.write(buffer, 0, bytes_read);
??????????? }
??????????? return (writer.toString());
??????? } finally {
??????????? if (null != is)
??????????????? is.close();
??????? }
??? }
最后調用velocity的方法
Velocity.evaluate(Context context, java.io.Writer out, java.lang.String logTag, java.lang.String instring)
從而生成文件。居然不知道velocity有這樣的方法,挺無知的,為了路徑焦頭爛額,終于得解了。總結一下技巧:
1、Xobject.class.getClassLoader().getResourceAsStream("/templates/xx.vm")相對路徑獲得流;
2、Velocity.evaluate(...)方法使用;
posted on 2006-11-28 14:21
野草 閱讀(3069)
評論(0) 編輯 收藏 所屬分類:
2shtv