Guava has a method similar to the one from Commons IOUtils that Willi aus Rohr mentioned:
import com.google.common.base.Charsets;import com.google.common.io.Files;// ...String text = Files.toString(new File(path), Charsets.UTF_8);
EDIT by PiggyPigletFiles#toString
is deprecated, and due for removal Octobor 2019. Instead useFiles.asCharSource(new File(path), StandardCharsets.UTF_8).read();
EDIT by Oscar Reyes
This is the (simplified) underlying code on the cited library:
InputStream in = new FileInputStream(file);byte[] b = new byte[file.length()];int len = b.length;int total = 0;while (total < len) { int result = in.read(b, total, len - total); if (result == -1) { break; } total += result;}return new String( b , Charsets.UTF_8 );
Edit (by Jonik): The above doesn't match the source code of recent Guava versions. For the current source, see the classes Files, CharStreams, ByteSource and CharSource in com.google.common.io package.