adam bien's blog

Java 11: Read a File into a String 📎

The in Java 11 introduced method Files.readString reads the entire content of a file into a String with "UTF-8" encoding:


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;    

@Test
public void readFileIntoString() throws IOException {
    String content = Files.readString(Paths.get("[FILE_NAME]"));
    assertNotNull(content);
}    

This is a significant improvement over Java 8.

Enjoy Java 11!