Library contents

Java Files.readString

Files.readString loads a small text file into one String. Choose the character encoding used by the writer so names such as Café survive the round trip.

A simple example

Files.readString

  1. Start with A file containing A
  2. Do this Read it as text
  3. Result "A"
Decoding the single ASCII byte yields the letter A.

Syntax

Java syntax
Files.readString(path, StandardCharsets.UTF_8)
JDK 21 declarations (2)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Write and read a UTF-8 report

A small application saves a human-readable note for the next time it opens. Choose the text encoding and read or write the file deliberately so the saved characters can be recovered as intended.

ResultCafé: 3
Main.java
import java.nio.file.*;
import java.nio.charset.StandardCharsets;

public class Main {
    public static void main(String[] args) throws Exception {
        Path file = Files.createTempFile("sales-", ".txt");
        try {
            Files.writeString(file, "Café: 3", StandardCharsets.UTF_8);
            System.out.println(Files.readString(file, StandardCharsets.UTF_8));
        } finally {
            Files.deleteIfExists(file);
        }
    }
}
Output
Café: 3

The writer and reader both use UTF-8. The accented letter survives, and finally removes only the temporary file created by the example.

Advertisements

Read a café pickup instruction

A café prints the customer's pickup instruction on a small label. The note contains an accented word, so both the terminal saving it and the printer reading it use UTF-8.

Resultcafé pickup
Main.java
import java.nio.file.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            Path note = desk.resolve("pickup.txt");
            Files.writeString(note, "café pickup");
            System.out.println(Files.readString(note));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
café pickup

The default charset for this overload is UTF-8 and the returned string contains the original accent.

Read the instructions for a parcel locker

A parcel locker prints the pickup instruction that its attendant saved earlier in the day. The printer needs the whole message, including the locker number and the closing time.

ResultLocker 8 closes at six.
Main.java
import java.nio.file.Files;

class Main {
    public static void main(String[] args) throws Exception {
        var note = Files.createTempFile("pickup-", ".txt");
        try {
            Files.writeString(note, "Locker 8 closes at six.");
            System.out.println(Files.readString(note));
        } finally {
            Files.deleteIfExists(note);
        }
    }
}
Output
Locker 8 closes at six.

Reading the saved text gives the printer the complete sentence. The example uses its own temporary file and removes it when finished.

What to watch for

Reading a large file into one String uses memory for the entire file. A buffered reader can process one line at a time.

Documentation for Files.readString