I/O Beginner

Default-charset file I/O to explicit StandardCharsets

Use Files readers and writers with StandardCharsets constants instead of platform-default charset behavior and charset-name lookup.

I/O
✕ Platform-default charset
try (Reader reader = new FileReader(path.toFile())) {
    return readAll(reader);
}
✓ Explicit standard charset
try (BufferedReader reader = Files.newBufferedReader(
        path, StandardCharsets.UTF_8)) {
    return readAll(reader);
}
See a problem with this code? Let us know.
🌍

Portable results

The same bytes decode identically on every machine.

🔒

Predefined constant

StandardCharsets.UTF_8 cannot contain a misspelled charset name.

🧩

Modern file API

Path, buffering, and encoding are expressed in one operation.

Old Approach
Platform-default charset
Modern Approach
Explicit standard charset
Since JDK
7
Difficulty
Beginner
Default-charset file I/O to explicit StandardCharsets
Available

Widely available since JDK 7 (July 2011)

Reader and writer constructors that rely on the default charset make file behavior depend on the runtime environment. Files.newBufferedReader() and newBufferedWriter() accept an explicit Charset, while StandardCharsets provides guaranteed constants such as UTF_8 without string lookup or UnsupportedCharsetException. This combines the explicit-file-encoding and Charset.forName("UTF-8") migrations into one coherent pattern.