To read the contents of a file into a String from the classpath
, you can use this:
private String resourceToString(String filePath) throws IOException, URISyntaxException
{
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath))
{
return IOUtils.toString(inputStream);
}
}
Note:
IOUtils
is part of Commons IO
.
Call it like this:
String fileContents = resourceToString("ImOnTheClasspath.txt");