@InjectResources medium post

String resourcePath = "com/adelean/junit/jupiter/resource.txt";
ClassLoader classLoader = getClass().getClassLoader();
              
StringBuilder textBuilder = new StringBuilder();
              
try (InputStream resourceAsStream = classLoader.getResourceAsStream(resourcePath);
     InputStreamReader streamReader = new InputStreamReader(resourceAsStream);
     BufferedReader bufferedReader = new BufferedReader(streamReader)) {
    int c = 0;
    while ((c = bufferedReader.read()) != -1) {
        textBuilder.append((char) c);
    }
} catch (IOException ioException) {
    // handle exception
}
              
String resourceContent = textBuilder.toString();
URL url = Resources.getResource("com/adelean/junit/jupiter/resource.txt");
try {
    String resourceContent = Resources.toString(url, StandardCharsets.UTF_8);
} catch (IOException ioException) {
    // handle exception
}

Similiar Collections