Search code examples
javastringresourcestext-files

Read resource text file to String in Java


Is there a way to read a text file in the resource into a String?

I suppose this is a popular requirement, but I couldn't find any utility after Googling.


Solution

  • Yes, Guava provides this in the Resources class. For example:

    URL url = Resources.getResource("foo.txt");
    String text = Resources.toString(url, StandardCharsets.UTF_8);