Last active
June 11, 2023 09:36
-
-
Save aoudiamoncef/275838c65e70b6dceb5d9a8918d54da0 to your computer and use it in GitHub Desktop.
Spring Boot read file from resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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