Skip to content

Instantly share code, notes, and snippets.

@GioF71
Created June 17, 2021 08:44
Show Gist options
  • Save GioF71/fb869130ae1483bf3ef4412106924e73 to your computer and use it in GitHub Desktop.
Save GioF71/fb869130ae1483bf3ef4412106924e73 to your computer and use it in GitHub Desktop.
private static <T> List<T> readFile(
String fileName,
Supplier<List<T>> listSupplier,
Function<String, T> converter) throws IOException {
List<T> list = listSupplier.get();
FileInputStream inputStream = null;
try {
ClassLoader classLoader = Puzzle01.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
inputStream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
list.add(converter.apply(line));
}
reader.close();
} finally {
inputStream.close();
}
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment