Course contentsShow
← Java
Lesson 2006 of 2,87040. File I/O: Character StreamsFree lesson

Encoding Errors and Replacement Characters

Handle encoding errors, unmappable characters, and the replacement character (U+FFFD).

Encoding Errors and Replacement Characters

You'll learn how Java handles characters that can't be encoded or decoded properly during text I/O operations.

When converting between bytes and characters, you'll sometimes encounter characters that cannot be represented in the target encoding, or byte sequences that don't form valid characters. Java needs a strategy for handling these encoding errors.

What Are Encoding Errors?

Two main scenarios cause problems:

  1. Unmappable characters: You're writing a character that doesn't exist in your chosen encoding (e.g., writing emoji to ASCII)
  2. Malformed input: You're reading bytes that don't form valid characters in the declared encoding (e.g., invalid UTF-8 sequences)

The Replacement Character

By default, Java uses the replacement character U+FFFD (�) when it encounters unmappable or malformed data. This visible "error marker" appears in your output instead of crashing your program.

Think of it like autocorrect replacing a word it doesn't recognize with "[?]" — you know something went wrong, but your document doesn't break.

How It Works in Practice

When you create an InputStreamReader or OutputStreamWriter with a Charset, Java silently replaces problematic characters with U+FFFD. For example:

  • Writing "Café ☕" to ASCII encoding: the emoji becomes �
  • Reading corrupted UTF-8 bytes: invalid sequences become �

This fail-soft behavior prevents exceptions but loses information. The original character is gone, replaced forever by the placeholder.

When You'll See This

Watch for replacement characters when:

  • Converting between encodings with different character sets
  • Reading files with incorrect encoding declarations
  • Processing damaged or corrupted text files
  • Handling user input from unknown sources

Key Takeaway: Java replaces unmappable or malformed characters with U+FFFD (�) by default during encoding/decoding, preventing crashes but indicating data loss.