← Back to Home

Character Streams

Character Streams in Java are used to read and write text data. They work with 16-bit Unicode characters, making them suitable for text files and internationalization.

This is a core Java I/O topic and a common interview question, often compared with Byte Streams.

What Are Character Streams?

  • Work with characters instead of bytes
  • Handle Unicode automatically
  • Support character encoding
  • Part of java.io package
  • Base classes:
    • Reader
    • Writer

When to Use Character Streams

  • ✔ Text files (.txt, .csv, .xml, .json)
  • ✔ Logs and configuration files
  • ✔ User-readable data
  • ✔ Language-specific characters
  • ❌ Not for binary data (images, audio, PDFs)

Character Stream Class Hierarchy

Input Side

Reader
 ├── FileReader
 ├── BufferedReader
 ├── InputStreamReader
 └── CharArrayReader
          

Output Side

Writer
 ├── FileWriter
 ├── BufferedWriter
 ├── OutputStreamWriter
 └── PrintWriter
          

Reader (Base Class)

Used to read characters.

Key Methods

int read();            // reads one character
int read(char[] c);    // reads multiple characters
void close();          // closes stream
✔ Returns -1 at end of stream
          

Writer (Base Class)

Used to write characters.

Key Methods

void write(int c);           // writes one character
void write(char[] c);        // writes char array
void write(String s);        // writes string
void flush();
void close();
          

FileReader (Reading Text Files)

FileReader fr = new FileReader("data.txt");
int ch;
while ((ch = fr.read()) != -1) {
    System.out.print((char) ch);
}
fr.close();
          
  • ✔ Reads character by character
  • ❌ No buffering → slower

FileWriter (Writing Text Files)

FileWriter fw = new FileWriter("output.txt");
fw.write("Hello Java");
fw.close();
          
  • ✔ Overwrites by default
  • ✔ Append mode available
FileWriter fw = new FileWriter("output.txt", true);
          

Buffered Character Streams (Recommended)

BufferedReader

  • Reads text efficiently
  • Supports line-by-line reading
BufferedReader br =
    new BufferedReader(new FileReader("data.txt"));
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}
br.close();
          
  • ✔ Very fast
  • ✔ Most commonly used

BufferedWriter

  • Writes text efficiently
BufferedWriter bw =
    new BufferedWriter(new FileWriter("out.txt"));
bw.write("Java I/O");
bw.newLine();
bw.close();
          

InputStreamReader & OutputStreamWriter (Bridge Classes)

  • Convert byte streams to character streams
  • Handle explicit character encoding
InputStreamReader isr =
    new InputStreamReader(new FileInputStream("data.txt"), "UTF-8");
✔ Important for encoding control
          

Character Streams vs Byte Streams (Interview Favorite)

Aspect Character Streams Byte Streams
Data unit Character (16-bit) Byte (8-bit)
Encoding ✔ Yes ❌ No
File type Text Binary
Base classes Reader / Writer InputStream / OutputStream
Example Text files Images, PDFs

Common Beginner Mistakes

  • Using character streams for binary data
  • Ignoring encoding issues
  • Not using buffering
  • Forgetting to close streams
  • Hardcoding file paths

Best Practices

  • Use BufferedReader / BufferedWriter for text files
  • Use InputStreamReader for encoding control
  • Use try-with-resources
  • Prefer NIO (Files.readAllLines) for modern code
try (BufferedReader br = new BufferedReader(new FileReader("a.txt"))) {
    // read logic
}
          

Interview-Ready Answers

Short Answer

Character streams are used to read and write text data in Java.

Detailed Answer

In Java, character streams are based on Reader and Writer classes and are used for handling text data. They support Unicode characters and character encoding, making them suitable for reading and writing text files, unlike byte streams which handle raw binary data.

Key Takeaway

Character Streams = Text Data Handling. Use them for human-readable content, prefer buffering for performance, and manage encoding explicitly when required.