Skip to content

Instantly share code, notes, and snippets.

@rivancic
Last active May 11, 2017 14:32
Show Gist options
  • Save rivancic/2724c39bf66160097afbac7df7c2815e to your computer and use it in GitHub Desktop.
Save rivancic/2724c39bf66160097afbac7df7c2815e to your computer and use it in GitHub Desktop.
Spring Resources

Load resources recursively with pattern

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
resolver.getResources("classpath*:some/package/name/**/*.xml");

Read Resource content to string

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public String getResourceContentAsString(Resource resource) {
    String resourceContent = null;
    try (Scanner scanner = new Scanner(resource.getInputStream(), StandardCharsets.UTF_8.name())) {
      resourceContent = scanner.useDelimiter("\\A").next();
    } catch (IOException e) {
      LOG.debug("Cant read content", e);
    }
    return resourceContent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment