Skip to main content
Java 11 (LTS) · Topic 3 of 6

Files.readString and writeString

Definition​

Java 11 added Files.readString and Files.writeString to read or write a whole text file in one line, with UTF-8 by default.

Example code​

Write a file and read it back

import java.nio.file.Files;
import java.nio.file.Path;

class Demo {
public static void main(String[] args) throws Exception {
Path file = Files.createTempFile("notes", ".txt");

Files.writeString(file, "Java 11 makes this easy\nSecond line");
String text = Files.readString(file);

System.out.println(text);
Files.delete(file);
}
}

// Output:
// Java 11 makes this easy
// Second line

Finished this page? Track your progress (saved only in your browser).