不关闭字符串器会导致泄漏吗?
2022-09-01 02:22:02
我意识到在java中GC最终会清理对象,但我问不关闭字符串编写器是否是不好的做法,目前我正在这样做:
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();
}
我应该关闭编写器并创建一个新的字符串,如下所示:
String result = "";
...
finally {
result = writer.toString();
writer.close();
}
这样做更好吗?