Quick revision
The short answer
new String(bytes) and FileReader without a charset use the platform default charset. Since JDK 18 (JEP 400) that default is UTF-8 everywhere, but on older JDKs it came from the operating system and locale, for example windows-1252 on an English Windows machine, so UTF-8 bytes written on Linux were decoded as windows-1252 and accented letters turned into two garbage characters. The fix is to always pass a charset, such as new String(bytes, StandardCharsets.UTF_8), Files.readString(path, UTF_8) or new FileReader(file, UTF_8), and to use a CharsetDecoder with CodingErrorAction.REPORT where bad data must be detected rather than silently replaced.
What to remember
In this answer, we saw that APIs such as new String(bytes) and FileReader without a charset use the platform default, which before JDK 18 came from the operating system, so UTF-8 bytes from Linux were decoded as windows-1252 on the older Windows agent and each accented letter became two garbage characters. JEP 400 made UTF-8 the default from JDK 18, but the fix is to name the charset everywhere, prefer the java.nio.file.Files methods, and use a reporting CharsetDecoder where bad input must be detected. file.encoding, native.encoding and stdout.encoding tell us what each JVM is doing.
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.
new String(bytes) and FileReader without a charset use the platform default charset. Since JDK 18 (JEP 400) that default is UTF-8 everywhere, but on older JDKs it came from the operating system and locale, for example windows-1252 on an English Windows machine, so UTF-8 bytes written on Linux were decoded as windows-1252 and accented letters turned into two garbage characters. The fix is to always pass a charset, such as new String(bytes, StandardCharsets.UTF_8), Files.readString(path, UTF_8) or new FileReader(file, UTF_8), and to use a CharsetDecoder with CodingErrorAction.REPORT where bad data must be detected rather than silently replaced.
Why this question is asked
Encoding bugs are among the most common cross-platform bugs in Java, and they hide well because they only appear when the data has a non-ASCII character and the machines disagree on defaults. A build agent on an older Windows image is a classic place for this. Interviewers ask it as a debugging question to see whether we can explain the garbled output exactly, not just say encoding issue, and fix the code so it no longer depends on the machine. In this answer, we will recap bytes, characters and charsets, reproduce the garbling byte by byte, see what JEP 400 changed, fix the code, detect bad input, and list the properties to check when diagnosing.
1A Quick Recap: Bytes, Characters and Charsets
Files and network messages contain bytes. Java Strings contain characters. A charset is the rule for converting between them. In UTF-8, the letter e with an acute accent, U+00E9, is two bytes, C3 A9. In windows-1252, a common Windows charset for Western languages, the same letter is one byte, E9, and the bytes C3 and A9 mean two different characters, A with a tilde and the copyright sign.
So the same bytes give different text depending on which charset reads them. Whenever bytes become a String, or a String becomes bytes, a charset is involved, whether we name it or not.
2Reproducing the Garbled Text
The Linux server writes cafe with an accent using UTF-8, its default. The Windows agent reads the file with code that names no charset, so on an older JDK it uses windows-1252.
5 messages in order. Play them, or step through them one at a time.
Neither side failed; they used different defaults.
Notice the pattern of the damage: one accented letter became exactly two odd characters. That is the signature of UTF-8 bytes decoded as a single-byte charset, and recognising it tells us the direction of the mistake immediately. The reverse mistake, windows-1252 bytes read as UTF-8, shows the replacement character U+FFFD instead, because E9 alone is not valid UTF-8.
3What JEP 400 Changed
Before JDK 18, the default charset, returned by Charset.defaultCharset(), was chosen at startup from the operating system and locale: usually UTF-8 on Linux and macOS, and a legacy code page such as windows-1252 on Windows. Every API without a charset parameter used it: new String(bytes), String.getBytes(), FileReader, FileWriter, InputStreamReader and PrintStream among them.
JEP 400, delivered in JDK 18, made UTF-8 the default charset on every platform. So on JDK 18 and later, this bug disappears between two machines running modern JDKs. But the build agent in the question runs an older build, and many fleets still mix JDK versions, so code that relies on the default is still fragile. The old behaviour can also be brought back on new JDKs with -Dfile.encoding=COMPAT. That is why the real fix is in the code, not in the JDK version.
4Fix: Always Name the Charset
Every API that converts bytes and characters has an overload that takes a Charset, and StandardCharsets provides constants, so there is no reason to rely on the default.
Notice that the writing side matters as much as the reading side. Files.readString and the other java.nio.file.Files methods use UTF-8 when no charset is given, on every JDK version that has them (readString arrived in Java 11), which makes them a safer default than the java.io classes. On Java 8, FileReader has no charset constructor, so we use new InputStreamReader(new FileInputStream(file), UTF_8) instead.
5Detecting Bad Input Instead of Hiding It
new String(bytes, charset) never fails. When it meets bytes that are invalid in that charset, it quietly substitutes the replacement character U+FFFD. That is convenient for display, but for data processing it means corrupted input passes through silently and gets stored. When we need to know, we use a CharsetDecoder configured to report errors.
With the strict decoder, an import job can reject or quarantine a file with a clear error instead of loading text full of replacement characters. Interestingly, Files.readString and Files.readAllLines are strict already, which is another reason to prefer them.
6What to Check When Diagnosing
When an encoding problem appears, a few system properties tell us what each JVM is doing. file.encoding is the default charset used by the APIs above. native.encoding, added in JDK 17, shows what the operating system would have chosen, which is what older JDKs used. stdout.encoding and stderr.encoding, added in JDK 19, show the charset used for System.out and System.err, which on Windows consoles is often a different code page again. sun.jnu.encoding is used for file names. Printing Charset.defaultCharset() at startup in both environments, together with these properties and the JDK version, usually explains the difference in a minute.
7Follow-up questions
Why did the problem only appear on the Windows agent?
Because on Linux both the old and new defaults were UTF-8, so writer and reader agreed. Only the older JDK on Windows used a legacy code page. Code that relies on defaults works wherever the defaults happen to match, which is why such bugs survive for years.
Is setting -Dfile.encoding=UTF-8 on the agent a good fix?
It is a useful stopgap on older JDKs and makes the default UTF-8 for most APIs, but it is not the real fix. Code that names its charset works everywhere, including environments we do not control, such as a customer's machine or a new CI image.
What about text sent over HTTP?
The same rule applies: the charset belongs in the Content-Type header, such as application/json; charset=UTF-8, and the code that reads the body should use it rather than a default. Frameworks such as Spring handle this when the header is present.
8Conclusion
In this answer, we saw that APIs such as new String(bytes) and FileReader without a charset use the platform default, which before JDK 18 came from the operating system, so UTF-8 bytes from Linux were decoded as windows-1252 on the older Windows agent and each accented letter became two garbage characters. JEP 400 made UTF-8 the default from JDK 18, but the fix is to name the charset everywhere, prefer the java.nio.file.Files methods, and use a reporting CharsetDecoder where bad input must be detected. file.encoding, native.encoding and stdout.encoding tell us what each JVM is doing.
Open with
The code depends on the platform default charset. new String with just bytes and FileReader without a charset both use Charset.defaultCharset. Before JDK 18 that default came from the OS and locale, so it was UTF-8 on the Linux server but something like windows-1252 on an older Windows agent.
Then, if they want more
The file was written as UTF-8, where e with an accent is two bytes, C3 A9, and the agent decoded each byte as a separate windows-1252 character, which is why every accented letter turned into two strange characters. That two-for-one pattern is the giveaway for UTF-8 read as a single-byte charset.
JEP 400 in JDK 18 made UTF-8 the default everywhere, but the agent runs an older JDK, and someone can still set file.encoding to COMPAT, so the real fix is in the code.
I would pass the charset explicitly everywhere: new String with StandardCharsets.UTF_8, Files.readString with UTF_8, the FileReader constructor that takes a charset from Java 11, and getBytes with UTF_8 on the writing side as well.
If the data quality matters, I would also note that new String with a charset silently replaces bad bytes with the replacement character, so for imports I would use a CharsetDecoder with CodingErrorAction.REPORT, or the Files methods, which throw MalformedInputException. And for diagnosis I would print file.encoding, native.encoding, stdout.encoding and the JDK version on both machines.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 18, Java 11, Java 17, Java 19
- 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 file written on a Linux server shows garbled accented characters when read on an older...": https://scrivio-interview-library.netlify.app/java/strings/fd00d4ee41/