Get a Resource File’s Contents

No Comments Blog

Here is a little snippet on how to get the contents of a resource file.

String filePath = this.getClass().getClassLoader().getResource("folder/file.txt").getFile();
String fileContent = new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);

And for reference here are the imports used
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

Leave a Reply

Your email address will not be published. Required fields are marked *