Thursday, November 1, 2018

java - Will not closing a stringwriter cause a leak?



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();
}


Is this better to do?


Answer




The javadoc is quite explicit:




Closing a StringWriter has no effect.




And a quick look at the code confirms it:



public void close() throws IOException {
}


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...