Quick revision
The short answer
On Java 11, FileWriter and similar classes without a charset argument use the platform default charset, which on Windows is usually a legacy code page such as Windows-1252, while developers' Macs default to UTF-8, so the bug never shows in development. Java 18 and later make UTF-8 the default everywhere, so on Java 21 the Windows server suddenly writes UTF-8, which fixes the export for UTF-8 readers but can break consumers that expected the old code page. The real fix is to pass the charset explicitly; -Dfile.encoding=COMPAT can restore the old default temporarily during the migration.
What to remember
In this answer, we saw that on Java 11 FileWriter without a charset uses the platform default, which is a legacy code page on Windows and UTF-8 on Macs, so the export was only wrong on the Windows server. Java 18 and later default to UTF-8 everywhere, which fixes this export but changes behaviour for anything that relied on the old default, and -Dfile.encoding=COMPAT can restore that default for a while. The fix that stays fixed is to pass the charset explicitly, test with non-ASCII data, and let a static check flag default-charset calls.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
On Java 11, FileWriter and similar classes without a charset argument use the platform default charset, which on Windows is usually a legacy code page such as Windows-1252, while developers' Macs default to UTF-8, so the bug never shows in development. Java 18 and later make UTF-8 the default everywhere, so on Java 21 the Windows server suddenly writes UTF-8, which fixes the export for UTF-8 readers but can break consumers that expected the old code page. The real fix is to pass the charset explicitly; -Dfile.encoding=COMPAT can restore the old default temporarily during the migration.
Why this question is asked
Works on my machine is the classic sign of hidden environment dependence, and the default charset is one of the most common examples in Java. An export of customer names that turns Muller with an umlaut into two odd symbols is a data quality problem with real consequences: mismatched records, rejected bank files, unhappy customers. Interviewers ask this question to see whether we can connect the symptom to the platform default, and whether we understand how the Java 18 change affects a system that is being upgraded. In this answer, we will recap how the default charset is chosen, reproduce the bug on Java 11, see what changes on Java 21, and fix it so it stays fixed.
1A Quick Recap of the Default Charset
Text in memory is a sequence of chars; a file is a sequence of bytes. Writing text to a file means encoding chars to bytes with a charset, a mapping such as UTF-8 or Windows-1252. Many Java APIs let us skip the charset: new FileWriter(file), new FileReader(file), new PrintWriter(file), new String(bytes) and text.getBytes(). When we skip it, Java uses the default charset of the running JVM.
Up to Java 17, that default was chosen at startup from the operating system's locale. On macOS and most Linux systems the locale is UTF-8, so the default was UTF-8. On Windows, the default followed the system's ANSI code page, which for Western European and US installs is Windows-1252, and different again for other regions. A batch server with a minimal Linux locale could even default to US-ASCII, which cannot represent accented letters at all.
2Reproducing the Bug on Java 11
The export code probably looks like this. It compiles everywhere and contains no mention of an encoding.
| Environment | Default charset | Bytes for u-umlaut | Read as UTF-8 |
|---|---|---|---|
| Mac or Linux, Java 11 | UTF-8 | C3 BC | correct |
| Windows, Java 11 | windows-1252 (typical) | FC | garbled |
| Any OS, Java 18 or later | UTF-8 | C3 BC | correct |
| Java 18+ with -Dfile.encoding=COMPAT on Windows | windows-1252 | FC | garbled, as before |
Notice that the code is identical on both machines; only the default differs. Developers test on Macs, where the default happens to match what the consumer expects, so every test passes. The bug appears only on the Windows batch server. If the names contain characters that Windows-1252 cannot represent at all, such as Polish or Greek letters, they are written as question marks and the information is lost, not just mis-encoded.
The mirror image also happens: a file written correctly in UTF-8 is read on Windows with new FileReader(file), decoded as Windows-1252, and each accented letter turns into two characters like the familiar A-tilde sequences.
3What Changes When We Upgrade to Java 21
Java 18 delivered JEP 400, which made UTF-8 the default charset on every platform, independent of the locale. So when the batch server moves to Java 21, the very same FileWriter code starts writing UTF-8 on Windows too. For our export, whose consumer reads UTF-8, the bug disappears without any code change.
But there is a catch. Somewhere in the fleet there may be a consumer that was built around the old behaviour, for example a legacy Windows tool that expects Windows-1252, or a job that reads files written years ago in the old encoding with a default-charset FileReader. After the upgrade those break in the opposite direction. An upgrade is therefore a behaviour change for any code that relies on the default, and we must find and test those paths rather than assume the upgrade only fixes things.
For a controlled migration, Java 18 and later accept -Dfile.encoding=COMPAT, which restores the old rule of deriving the default from the operating system locale. That lets us upgrade the JVM first and change encodings later, one consumer at a time. It should be a temporary switch with an owner and a date, not a permanent setting.
4The Fix That Does Not Depend on the JVM
The lasting fix is to say which charset the file format uses, in the code, every time. Since Java 11 FileWriter and FileReader have constructors that take a Charset, and the Files methods have taken one since Java 7.
Notice that Files.newBufferedWriter reports characters it cannot encode by throwing an exception, while FileWriter silently replaces them with question marks. Failing loudly is usually better for exports, because silent replacement is how names get corrupted without anyone noticing.
Finally, we should agree the encoding with the consumer and write it into the interface description of the export. If the consumer needs to detect the encoding itself, some formats such as CSV for spreadsheet tools are commonly written with a UTF-8 byte order mark, but that is a choice to make with the consumer, not by default.
5Preventing It From Coming Back
We add a test that writes non-ASCII names and reads the bytes back with an explicit charset, and we run the build on more than one platform or with a non-UTF-8 default forced, for example by running a test JVM with -Dfile.encoding=COMPAT on a locale such as en_US.ISO-8859-1 on Linux. A static check such as Error Prone's DefaultCharset rule flags every constructor or method that relies on the default, so new code cannot reintroduce the problem.
6Follow-up questions
How can I see the default charset a server is using?
Print Charset.defaultCharset() at startup, or run java -XshowSettings:properties -version and look at file.encoding, native.encoding (Java 17 and later), and on newer versions stdout.encoding. On Java 18 and later file.encoding shows UTF-8 unless COMPAT was set, and native.encoding shows what the operating system would have chosen.
Would setting -Dfile.encoding=UTF-8 on the Java 11 server have fixed it?
In practice it usually did, and many teams relied on it, but on Java 11 setting that property was not an officially supported way to change the default and some parts of the JDK did not honour it consistently. It also leaves the code dependent on a startup flag that the next deployment script might drop. Passing the charset in code is the dependable fix.
7Conclusion
In this answer, we saw that on Java 11 FileWriter without a charset uses the platform default, which is a legacy code page on Windows and UTF-8 on Macs, so the export was only wrong on the Windows server. Java 18 and later default to UTF-8 everywhere, which fixes this export but changes behaviour for anything that relied on the old default, and -Dfile.encoding=COMPAT can restore that default for a while. The fix that stays fixed is to pass the charset explicitly, test with non-ASCII data, and let a static check flag default-charset calls.
Open with
The cause is the platform default charset. On Java 11, FileWriter, FileReader, new String(bytes) and getBytes without a charset use the default, which comes from the operating system locale.
Then, if they want more
On Macs that is UTF-8, so developers never see a problem, but on a Windows server it is typically a legacy code page like Windows-1252. So a u-umlaut is written as one byte, FC, and a consumer reading UTF-8 shows garbage, or characters the code page cannot hold become question marks.
Upgrading to Java 21 changes this because of JEP 400 in Java 18: UTF-8 is the default on every platform. That makes this export correct without a code change, but it is a behaviour change, so any consumer or old file that depended on the Windows code page now breaks the other way.
During a migration, -Dfile.encoding=COMPAT restores the old default, but I would treat it as temporary. The real fix is to pass the charset in code: new FileWriter(file, StandardCharsets.UTF_8) or Files.newBufferedWriter(path, UTF_8), and agree the encoding with the consumer.
I would add a test that writes and reads back non-ASCII names, and turn on a static check like Error Prone's DefaultCharset so no new code relies on the default.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11, Java 18, Java 21, Java 17
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A service's file export works on developers' Macs but produces garbled non-ASCII names...": https://scrivio-interview-library.netlify.app/java/io-and-nio/c8833747e2/