What is simplest way to read a file into String?

String content = new Scanner(new File("D:\\test.txt")).useDelimiter("\\Z").next();

OR

byte[] bytes = Files.readAllBytes(Paths.get("D:\\test.txt")); String str = new String(bytes, StandardCharsets.UTF_8);

Using Scanner Class

The following Java code will read an entire file into the String. Example
import java.io.*; import java.util.Scanner; public class TestClass{ public static void main(String[] args) { try{ String content = new Scanner(new File("D:\\test.txt")).useDelimiter("\\Z").next(); System.out.println(content); }catch(Exception e){ System.out.println(e); } } }
Note : useDelimiter("\\Z") is set the delimiter to the end of the file

Java NIO

Java NIO (New IO) is an alternative IO API for Java (from Java 1.4), meaning alternative to the standard Java IO and Java Networking API's.

Stream Oriented vs. Buffer Oriented

Java IO being stream oriented means that you read one or more bytes at a time, from a stream. What you do with the read bytes is up to you. Java NIO's buffer oriented approach is slightly different. Data is read into a buffer from which it is later processed. The following Java code is a compact, robust idiom for Java 7 , wrapped up in a utility method. Full Source
import java.nio.file.Files; import java.nio.file.Paths; import java.nio.charset.StandardCharsets; public class TestClass{ public static void main(String[] args) { try{ byte[] bytes = Files.readAllBytes(Paths.get("D:\\test.txt")); String str = new String(bytes, StandardCharsets.UTF_8); System.out.println(str); }catch(Exception e){ System.out.println(e); } } }