30
30
import java .nio .charset .Charset ;
31
31
import java .util .List ;
32
32
import java .util .Properties ;
33
+ import java .util .function .Consumer ;
33
34
34
35
/**
35
36
* Various utility methods for working with files, resources and streams.
@@ -241,14 +242,7 @@ public static Properties loadProperties(final URL url) throws IOException {
241
242
*/
242
243
public static String loadTextFile (final String location ) throws IOException {
243
244
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 );
252
246
return builder .toString ();
253
247
}
254
248
@@ -260,17 +254,19 @@ public static String loadTextFile(final String location) throws IOException {
260
254
* @return the List of Strings with the lines of the file appended
261
255
* @throws IOException if an I/O error occurs
262
256
*/
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
+ }
266
261
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
+ }
271
269
}
272
-
273
- return list ;
274
270
}
275
271
276
272
/**
0 commit comments