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.nio.file.Files;
import java.nio.file.Paths;    

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

This is a significant improvement over Java 8.

Enjoy Java 11!


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

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

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