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!

Comments:

I think `Path.of` is preferred to `Paths.get`

Posted by 83.54.143.4 on April 11, 2020 at 10:07 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License