本文介绍了关闭字符串写入器会不会导致泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到在java中GC最终会清理对象,但我问的是不关闭你的字符串编写器是不好的做法,目前我这样做:

I realize that in java the GC will eventually cleanup objects, but I'm asking if it is bad practice to not close your string writer, currently I am doing this:

 private static String processTemplate(final Template template, final Map root) {
        StringWriter writer = new StringWriter();
        try {
            template.process(root, writer);
        } catch (TemplateException e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        finally {

        }

        return writer.toString();
    }

我应该关闭编写器并创建一个像这样的新字符串:

Should I be closing the writer and creating a new String like this:

String result = "";

...

finally {
  result = writer.toString();
  writer.close();
}

这样做更好吗?

推荐答案

非常明确:

快速查看确认:

public void close() throws IOException {
}

这篇关于关闭字符串写入器会不会导致泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 03:16