Skip to content

Commit 05eff68

Browse files
committed
ResourceUtil: Close text file resources.
1 parent 6f411b7 commit 05eff68

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

metafacture-commons/src/main/java/org/metafacture/commons/ResourceUtil.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.charset.Charset;
3131
import java.util.List;
3232
import java.util.Properties;
33+
import java.util.function.Consumer;
3334

3435
/**
3536
* Various utility methods for working with files, resources and streams.
@@ -241,14 +242,7 @@ public static Properties loadProperties(final URL url) throws IOException {
241242
*/
242243
public static String loadTextFile(final String location) throws IOException {
243244
final StringBuilder builder = new StringBuilder();
244-
final BufferedReader reader = new BufferedReader(getReader(location));
245-
246-
String line = reader.readLine();
247-
while (line != null) {
248-
builder.append(line);
249-
line = reader.readLine();
250-
}
251-
245+
loadTextFile(location, builder::append);
252246
return builder.toString();
253247
}
254248

@@ -260,17 +254,19 @@ public static String loadTextFile(final String location) throws IOException {
260254
* @return the List of Strings with the lines of the file appended
261255
* @throws IOException if an I/O error occurs
262256
*/
263-
public static List<String> loadTextFile(final String location,
264-
final List<String> list) throws IOException {
265-
final BufferedReader reader = new BufferedReader(getReader(location));
257+
public static List<String> loadTextFile(final String location, final List<String> list) throws IOException {
258+
loadTextFile(location, list::add);
259+
return list;
260+
}
266261

267-
String line = reader.readLine();
268-
while (line != null) {
269-
list.add(line);
270-
line = reader.readLine();
262+
private static void loadTextFile(final String location, final Consumer<String> consumer) throws IOException {
263+
try (BufferedReader reader = new BufferedReader(getReader(location))) {
264+
String line = reader.readLine();
265+
while (line != null) {
266+
consumer.accept(line);
267+
line = reader.readLine();
268+
}
271269
}
272-
273-
return list;
274270
}
275271

276272
/**

0 commit comments

Comments
 (0)