Recently I needed a simple method to load a file from resources into a String. Even if it looks like a very simple task, it usually involves many awkward transformations and exceptions catching.
This solution uses a Scanner
to read the entire InputStream
into a String
object.
private String loadFromResources(String resourcePath) throws Exception {
try (InputStream is = getClass()
.getClassLoader().getResourceAsStream(resourcePath)) {
Scanner sc = new Scanner(is);
sc.useDelimiter("\\A");
return sc.next();
}
}
The code takes advantage of Java7 AutoCloseable interface and try-with-resources statement to reduce boilerplate code. In case of exceptions the underlying input stream is closed automatically.
It works by setting \A
as a delimiter. This regexp Pattern
looks for the “beginning of the input” token which of course can’t be reached
and causes all the input to be picked up by next()
.
Using the actual class’ classloader works very well in OSGi environments too (e.g. Apache Karaf, JBoss Fuse).