Practical technical guides / Core IT: Java

Java's UTF-8 Default Can Corrupt Legacy Files After an Upgrade

JEP 400 changed the default charset in Java 18. A JDK 23 Windows probe separates legacy-file decoding, explicit charsets and console encoding.

By Sandeep Belgavi · Published 2026-12-02

JEP 400 changed the default charset in Java 18. A JDK 23 Windows probe separates legacy-file decoding, explicit charsets and console encoding.

The default charset is no longer the machine charset

Before Java 18, the default charset of a Java program came from the operating system. On a Linux server with a UTF-8 locale it was UTF-8; on a Windows laptop with an English locale it was usually windows-1252; on a container image with no locale set it could be US-ASCII. Every API that turns bytes into characters without being told which charset to use picked up that value: FileReader, FileWriter, InputStreamReader with one argument, String.getBytes() with no argument, new String(bytes), Scanner on a stream, and more. The same program could read a file correctly on one machine and corrupt it on another.

JEP 400 changed that in JDK 18. It standardizes the default charset used by the affected Java APIs on UTF-8 while preserving console-specific behavior. Most teams moved from Java 11 or 17 straight to 21 and never read the JEP, so the change shows up as a bug report: a report export from Windows that suddenly has replacement characters, or a test that passed on a developer laptop and now fails, or the reverse.

Measured on JDK 23 on Windows

A small probe was compiled and run on JDK 23.0.2 on Windows 11 with an English system locale, output redirected to a file. The input file holds eight bytes, the windows-1252 encoding of the text Café £5 followed by a newline: 43 61 66 E9 20 A3 35 0A. In windows-1252, E9 is é and A3 is £. Neither byte is valid UTF-8 on its own.

Read or propertyDefault runWith -Dfile.encoding=COMPAT
Charset.defaultCharset()UTF-8windows-1252
file.encodingUTF-8Cp1252
native.encodingCp1252Cp1252
sun.jnu.encoding (file names)Cp1252Cp1252
stdout.encoding and stderr.encodingCp1252Cp1252
new FileReader(file)C a f U+FFFD space U+FFFD 5Café £5
Files.newBufferedReader(file, windows-1252)Café £5Café £5
Files.readAllLines(file)MalformedInputExceptionMalformedInputException
"Café".getBytes().length54

Passing -Dfile.encoding=UTF-8 gave exactly the same results as the default run.

Four points come out of this table.

First, the default changed but the platform did not. The operating system still reports windows-1252, and Java now exposes that as native.encoding, a property JEP 400 notes was introduced in JDK 17. Code that genuinely needs the platform encoding, for example to read a file written by a native tool on the same machine, can ask for it explicitly.

Second, the failure is silent. FileReader with the default charset did not throw; it replaced é and £ with U+FFFD, the replacement character. A program that reads a legacy file, transforms it and writes it out again loses the original characters permanently, and nothing in the logs says so.

Third, Files.readAllLines and the other java.nio.file.Files methods that take no charset have always used UTF-8, before and after JDK 18, and they report malformed input with an exception instead of replacing it. That is why the same file read two ways behaves differently in the same program.

Fourth, writing changes too. "Café".getBytes() produced five bytes, because é is two bytes in UTF-8. A fixed-width record format, a checksum over the bytes, or a byte-length limit on a column all see a different number after the upgrade.

Where the platform encoding still applies

The default charset is not a universal encoding switch. Separate console behavior, native interfaces and explicitly selected charsets:

  • Console output. System.out and System.err use Console.charset() when a console exists and the stdout.encoding and stderr.encoding properties otherwise, as the JDK 23 System documentation describes. In the probe both were Cp1252, so a program that prints é to a redirected stream writes the windows-1252 byte, not UTF-8.
  • Native path interfaces. sun.jnu.encoding stayed Cp1252 in the probe, but that property alone does not prove how Windows represents a file name: Windows native path APIs use Unicode. Do not infer file-content encoding or portable path behavior from this internal property.
  • Explicit requests. Anything that passes a charset, or asks for native.encoding, does what it is told.

The javac compiler is affected as well. The JEP notes that javac assumes source files use the default charset unless told otherwise with -encoding. A source file saved as windows-1252 with non-ASCII characters in string literals compiles to different strings on JDK 18 or later. The JEP recommends checking by compiling with javac -encoding UTF-8 on the current JDK before moving.

Worked example: the nightly order export

Belgavi's Book Store exports book orders to a CSV file each night. Reviewer A's team wrote the exporter years ago on Java 11, running on a Windows server. It uses new FileWriter(path) and nothing else. A second job, owned by another team, reads the CSV with new FileReader(path) and loads it into reporting.

On Java 11 both jobs used windows-1252, the file was windows-1252, and the reader decoded it correctly. Titles with accented author names were fine. Then the reader job moved to Java 21 while the exporter stayed on Java 11 for another month. The reader now decoded with UTF-8 and every accented character in the existing files became U+FFFD. When the exporter also moved, new files were UTF-8 and read correctly again, but every file written in between was damaged in the reporting tables. Nobody saw an exception at any point.

The fix is not to pick a better default but to stop relying on one.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

final class OrderCsv {
    // Files written before the migration date are windows-1252; later files are UTF-8.
    static final Charset LEGACY = Charset.forName("windows-1252");

    static BufferedWriter writer(Path path) throws IOException {
        return Files.newBufferedWriter(path, StandardCharsets.UTF_8);
    }

    static BufferedReader reader(Path path, boolean legacyFile) throws IOException {
        return Files.newBufferedReader(path, legacyFile ? LEGACY : StandardCharsets.UTF_8);
    }
}

Recording which files are legacy matters more than the code. A file does not say what encoding it uses. The team added the charset to the export manifest and to the file name, so the reader never has to guess.

Migration steps

  1. Find every implicit use. Search for new FileReader(, new FileWriter(, new InputStreamReader( with one argument, new OutputStreamWriter( with one argument, getBytes() with no argument, new String( on a byte array without a charset, new Scanner( on a stream or file without a charset, and new PrintStream( on a stream. Static analysis rules for default encoding, such as the ones in SpotBugs and Error Prone, find most of them.
  2. Decide for each use what the bytes really are. Files the program wrote itself, on any machine, should be UTF-8. Files from other systems should use whatever charset that system writes, named explicitly. Only output for a local native tool should use native.encoding.
  3. Pass the charset explicitly. From JDK 11, FileReader and FileWriter accept a charset; from JDK 10, so do Scanner and PrintStream. The Files methods take one everywhere.
  4. Run the test suite twice on your current JDK, once with -Dfile.encoding=UTF-8 and once with -Dfile.encoding=COMPAT on JDK 18 or later, or with the old platform value on earlier JDKs. Any test whose result depends on the flag still has an implicit use.
  5. Compile with javac -encoding UTF-8 explicitly and configure the build tool to do the same, so source encoding never depends on the build machine.
  6. Inventory data at rest. For files written by the old code on non-UTF-8 platforms, either convert them once with a recorded charset or keep reading them with the legacy charset.

-Dfile.encoding=COMPAT is a stopgap that restores the pre-18 behaviour for the whole process. It is useful for one release while you fix the code, and it hides the problem again on the next machine that has a different locale.

Failure modes

  • Mixed fleets. As in the example, a writer and a reader on different sides of JDK 18 disagree for as long as both run. Make both explicit before upgrading either.
  • Replacement instead of exceptions. Decoders created through the reader classes replace malformed input. If you need to know, create a CharsetDecoder with CodingErrorAction.REPORT.
  • Console output that looks fine. On a terminal that shows UTF-8 correctly, the program may still write Cp1252 bytes to a redirected file, as the probe did. Set stdout.encoding or write through an explicit writer when the bytes matter.
  • Containers with no locale. Before JDK 18 these often defaulted to ASCII and the upgrade silently fixed them. If a workaround such as -Dfile.encoding=UTF-8 sits in a start script, it is now harmless, but COMPAT would bring the ASCII default back.
  • Byte limits. Database columns, message size limits and fixed-width formats measured in bytes overflow when accented text grows.

When nothing changes

Programs that only run on Linux and macOS with UTF-8 locales already used UTF-8, and JEP 400 changes nothing for them. The same is true of code that already passes a charset everywhere. The risk is concentrated on Windows hosts, on older containers and on code that reads files from systems you do not control.

How to verify it

The companion example default_charset_probe contains the probe program, the eight-byte input file and a script that runs it three times with the flags above and compares the output to recorded expectations. Run it on each JDK and operating system you deploy to, and keep the output with the upgrade notes. Then run your own test suite under both flag values and treat any difference as a bug to fix before the upgrade.

Primary references and authorship

Sources checked 2026-12-02. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/java/default_charset_probe. Views are my own and do not represent any employer, past or present.