Utils to read resource text file to String (Java)

Loc Phan picture Loc Phan · May 20, 2011 · Viewed 228k times · Source

Is there any utility that helps 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.

Answer

Jon Skeet picture Jon Skeet · May 20, 2011

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

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