[Bugfix] fixed a resource leak where a BufferedReader was not close

In FileHelper.readFileToString() the BufferedReader was not closed. This
has been corrected
This commit is contained in:
Robert von Burg 2012-07-28 22:50:52 +02:00
parent c65fe9c247
commit 7dbd5af6a7
1 changed files with 11 additions and 1 deletions

View File

@ -60,9 +60,11 @@ public class FileHelper {
* @return the contents of a file as a string * @return the contents of a file as a string
*/ */
public static final String readFileToString(File file) { public static final String readFileToString(File file) {
BufferedReader bufferedReader = null;
try { try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); bufferedReader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String line; String line;
@ -77,6 +79,14 @@ public class FileHelper {
throw new RuntimeException("Filed does not exist " + file.getAbsolutePath()); throw new RuntimeException("Filed does not exist " + file.getAbsolutePath());
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Could not read file " + file.getAbsolutePath()); throw new RuntimeException("Could not read file " + file.getAbsolutePath());
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
logger.error("Failed to close BufferedReader: " + e.getLocalizedMessage());
}
}
} }
} }