In Java, Files.readString(path) reads the entire file and returns its text as one String, decoding the bytes as UTF-8. Line breaks stay inside the string exactly as they are in the file.
A second overload takes a Charset for files in another encoding. It is the reading half of writeString, and it is meant for files that comfortably fit in memory.
A static method of the final utility class java.nio.file.Files, available since Java 11. Call it on the class name, as in Files.readString(...), after import java.nio.file.Files;. You never create a Files object.
Requires Java 11 or later.
On Java 8, read the bytes and decode them yourself: new String(Files.readAllBytes(path), StandardCharsets.UTF_8). Example 3 shows that both give the same result.
Syntax
Pass the path, and optionally a charset:
Java
String text = Files.readString(path); // UTF-8
String latin = Files.readString(path, StandardCharsets.ISO_8859_1);
String java8 = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
Call
What it does
Files.readString(p)
Whole file as UTF-8 text.
Files.readString(p, cs)
Whole file in the given charset.
Files.readAllLines(p)
Text split into a List of lines instead.
Since Java 11. Unlike new String(bytes, cs), which silently replaces bad bytes with a replacement character, readString refuses to guess and throws.
Library declarations
Java
public static String readString(Path path) throws IOException
public static String readString(Path path, Charset cs) throws IOException
The line break is kept inside the returned string.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class ReadStringBasic {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path note = dir.resolve("note.txt");
Files.write(note, "Line one\nLine two".getBytes(StandardCharsets.UTF_8));
String text = Files.readString(note);
System.out.println(text);
System.out.println("Characters: " + text.length());
System.out.println("Lines: " + text.split("\n").length);
Files.delete(note);
Files.delete(dir);
}
}
Output
Run with java ReadStringBasic. Requires Java 11 or later; the output is the same on Windows, macOS, and Linux.
Line one
Line two
Characters: 17
Lines: 2
How It Works
One call, all text.readString opens the file, decodes every byte as UTF-8, and closes it again.
Breaks are characters. Printing the string shows two lines, because the \n from the file is part of it.
Count it. Two 8-character lines plus one line break give 17 characters, and splitting at the break gives 2 parts.
Example 2 — Invalid UTF-8 vs ISO-8859-1
A file written in an old 8-bit encoding is not valid UTF-8. The default overload refuses it; the charset overload reads it.
Java
import java.io.IOException;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class ReadStringCharset {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path old = dir.resolve("latin1.txt");
Files.write(old, new byte[] {'c', 'a', 'f', (byte) 0xE9});
try {
Files.readString(old);
} catch (MalformedInputException e) {
System.out.println("As UTF-8: MalformedInputException");
}
String word = Files.readString(old, StandardCharsets.ISO_8859_1);
System.out.println("As ISO-8859-1: " + word.length() + " characters");
System.out.println("Last character code: " + (int) word.charAt(3));
Files.delete(old);
Files.delete(dir);
}
}
Output
Run with java ReadStringCharset. Requires Java 11 or later; the output is the same on Windows, macOS, and Linux.
As UTF-8: MalformedInputException
As ISO-8859-1: 4 characters
Last character code: 233
How It Works
Old-style bytes. 0xE9 is e-acute in ISO-8859-1, but in UTF-8 it starts a multi-byte sequence that never finishes.
Strict by default. The UTF-8 overload throws MalformedInputException rather than inventing a replacement character.
Right charset, right text. Decoding as ISO-8859-1 gives four characters, and the last one has code 233, which is e-acute. The code is printed instead of the letter to keep the output ASCII.
Example 3 — readString Next to the Java 8 Way
Read the same file both ways and confirm the strings are equal.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class ReadStringJava8 {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path colors = dir.resolve("colors.txt");
Files.write(colors, "red green blue".getBytes(StandardCharsets.UTF_8));
String modern = Files.readString(colors);
String classic = new String(Files.readAllBytes(colors), StandardCharsets.UTF_8);
System.out.println("Same text: " + modern.equals(classic));
System.out.println("Words: " + modern.split(" ").length);
Files.delete(colors);
Files.delete(dir);
}
}
Output
Run with java ReadStringJava8. Requires Java 11 or later; the output is the same on Windows, macOS, and Linux.
Same text: true
Words: 3
How It Works
Java 11 line.Files.readString(colors) reads and decodes in one step.
Java 8 line.readAllBytes plus new String(bytes, UTF_8) does the same work in two steps and also compiles on Java 7 and 8.
Equal results. For valid UTF-8 both strings match; the only difference appears with invalid bytes, where the Java 8 form quietly substitutes characters.
Frequently Asked Questions
Java 11, together with Files.writeString.
Use new String(Files.readAllBytes(path), StandardCharsets.UTF_8).
UTF-8, unless you call the overload that takes a Charset.
The file contains bytes that are not valid in the charset, often because it was saved in an older encoding such as ISO-8859-1 or Windows-1252. Pass the right charset.
Only if the file fits in memory, and never above about 2 GB. Stream large files with Files.lines or Files.newBufferedReader.