Byte Streams vs Character Streams (InputStream vs Reader)
1Concept
Java I/O is split into two distinct hierarchies: Byte Streams (InputStream / OutputStream) operate on raw 8-bit bytes (audio, images, PDFs), whereas Character Streams (Reader / Writer) operate on 16-bit Unicode characters with automatic character set encoding translation.
2Architecture Diagram
Binary File (PNG/ZIP) ---> [ FileInputStream ] ---> Read raw bytes (0x89, 0x50, 0x4E...)
Text File (UTF-8) ---> [ FileReader / BufferedReader ] ---> Decodes to char ('H', 'e', 'l', 'l', 'o')3Code Example
Core Java
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
public class StreamHierarchyDemo {
public static void main(String[] args) throws Exception {
byte[] binaryData = "Java Platform I/O Streams".getBytes(StandardCharsets.UTF_8);
// 1. Byte Stream reading raw bytes
InputStream byteStream = new ByteArrayInputStream(binaryData);
System.out.println("Byte stream first byte: " + byteStream.read());
// 2. Character Stream bridging bytes to chars
byteStream.reset();
Reader charReader = new InputStreamReader(byteStream, StandardCharsets.UTF_8);
System.out.println("Character stream first char: " + (char) charReader.read());
}
}4Expected Output
Byte stream first byte: 74 Character stream first char: J
5Key Takeaways
- ✓InputStream / OutputStream are the root classes for 8-bit byte streams.
- ✓Reader / Writer are the root classes for 16-bit character streams.
- ✓InputStreamReader serves as the bridge between byte streams and character streams.