Skip to content

Instantly share code, notes, and snippets.

@aoudiamoncef
Last active June 11, 2023 09:36
Show Gist options
  • Save aoudiamoncef/275838c65e70b6dceb5d9a8918d54da0 to your computer and use it in GitHub Desktop.
Save aoudiamoncef/275838c65e70b6dceb5d9a8918d54da0 to your computer and use it in GitHub Desktop.
Spring Boot read file from resources
public String loadFile(final String pathResource) {
try (final InputStream is = new ClassPathResource(pathResource).getInputStream()) {
// To be customized
return IOUtils.toString(is, "UTF-8");
} catch (final Exception e) {
final String errorMessage = "Error loading file ";
// Could be more specific
throw new RuntimeException(errorMessage + e);
}
}
public String readResource(final String pathResource) {
try (final InputStream is = new ClassPathResource(pathResource).getInputStream()) {
return IOUtils.toString(is, "UTF-8");
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}
public String readResourceParallel(final String pathResource) {
try (final InputStream is = new ClassPathResource(pathResource).getInputStream()) {
return new BufferedReader(new InputStreamReader(is)).lines()
.parallel().collect(Collectors.joining("\n"));
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment