Velocity Template合并方法接受StringWriter但buff大小不够

时间:2015-04-15 11:10:28

标签: java velocity stringwriter

我试图从包含超过5000行和超过10个工作表的excel文件中读取使用Apache POI,我获取所有列和行值并将其存储在ArrayListMultiMap>中,这个逻辑工作得很好,然后我将这个对象存储在VelocityContext中,如下所示:

       VelocityContext context = new VelocityContext();
        context.put("excelMap", excelMap);

我需要在速度模板中使用此集合,通过迭代excelMap来填充带有值的XML文件:

     VelocityEngine vEngine = new VelocityEngine();
    vEngine.setProperty("resource.loader", "class");
    vEngine.setProperty("class.resource.loader.class",  "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    Template t = vEngine.getTemplate(vmTemplate);
    **StringWriter sw = new StringWriter(excelMap.size());**
    **t.merge(context,sw)**

但是,StringWriter容量总是1024,并且已经尝试将PrintWriter与BufferedWriter,StringBuilderWriter结合使用,当应用程序到达合并行时,它会挂起并最终显示OutOfMemory异常。

任何人都可以帮助我,我已经准备好所有与StringWriter和PrintWriter相关的帖子,但没有运气。

谢谢

1 个答案:

答案 0 :(得分:0)

1024限制是红鲱鱼。因此,您不会出现内存不足错误。如果Writer超过初始容量,将分配更多内存。你真正的问题是电子表格和数据,而不是Velocity。

来自StringWriter javadocs:

initialSize - 在自动展开之前适合此缓冲区的char值的数量(强调我的)

您应该尝试将一个工作表或一个条目一次转换为XML,而不是将整个电子表格读入内存。可能Velocity不是您的最佳选择。