Java Files readString() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 11+

Definition

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);
CallWhat 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

Parameters

ParameterDescription
pathThe file to read.
cs(Second overload) The charset to decode with.

Return Value

Return ValueDescription
StringThe complete file content, line breaks included.

Compare with Similar Methods

Ways to read text:

MethodHow it differs
readAllLines()Java 7/8; one list element per line.
readAllBytes()Java 7; raw bytes, decode yourself.
lines()Java 8; lazy stream for large files.

Java Examples

Example 1 — Read a Two-Line File into a String

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);
    }
}

How It Works

  1. One call, all text. readString opens the file, decodes every byte as UTF-8, and closes it again.
  2. Breaks are characters. Printing the string shows two lines, because the \n from the file is part of it.
  3. 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);
    }
}

How It Works

  1. Old-style bytes. 0xE9 is e-acute in ISO-8859-1, but in UTF-8 it starts a multi-byte sequence that never finishes.
  2. Strict by default. The UTF-8 overload throws MalformedInputException rather than inventing a replacement character.
  3. 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);
    }
}

How It Works

  1. Java 11 line. Files.readString(colors) reads and decodes in one step.
  2. 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.
  3. 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.

Next: Files.readSymbolicLink()

Read the target stored in a symbolic link, without following it.

readSymbolicLink() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful