Whole text file to a String in Java

apache commons-io has:

String str = FileUtils.readFileToString(file, "utf-8");

But there is no such utility in the standard java classes. If you (for some reason) don’t want external libraries, you’d have to reimplement it. Here are some examples, and alternatively, you can see how it is implemented by commons-io or Guava.

Leave a Comment