🔄 Commonalities
Feature InputStream / OutputStream Reader / Writer Belong to java.io
packagejava.io
packageAbstract classes Yes ✅ Yes ✅ Base for stream hierarchy Yes ✅ Yes ✅ Support chaining/wrapping Yes ✅ Yes ✅ (e.g., with buffering) Need to be closed Yes ✅ (implements Closeable
) Yes ✅ (implements Closeable
) Support try-with-resources Yes ✅ Yes ✅
So all four are abstract base classes that handle I/O streams , support stream composition (via decorators like Buffered*
), and must be explicitly closed (or auto-closed with try-with-resources).
📊 Key Differences
Feature InputStream
OutputStream
Reader
Writer
📦 Type Byte-based Byte-based Character-based Character-based 🔁 Direction Read Write Read Write 🔢 Operates on byte
(8-bit)byte
(8-bit)char
(16-bit Unicode)char
(16-bit Unicode)🧠 Use Case Read binary data (e.g. files, images) Write binary data Read text (files, strings) Write text 🎯 Example Classes FileInputStream
, BufferedInputStream
FileOutputStream
, PrintStream
BufferedReader
, FileReader
BufferedWriter
, PrintWriter
🎨 Visual Summary
Byte Streams (Raw Data)
┌──────────────┐ ┌──────────────┐
│ InputStream │◄────Read──┤ Binary Source│
└──────────────┘ └──────────────┘
┌──────────────┐ ┌──────────────┐
│OutputStream │───Write──►│ Binary Sink │
└──────────────┘ └──────────────┘
Character Streams (Text Data)
┌────────┐ ┌──────────────┐
│ Reader │◄────Read──┤ Text Source │
└────────┘ └──────────────┘
┌────────┐ ┌──────────────┐
│ Writer │───Write──►│ Text Sink │
└────────┘ └──────────────┘
🧪 Example Use Cases
Task Best Option Read a binary file (e.g. image) InputStream
Write raw bytes to a socket OutputStream
Read lines from a text file BufferedReader
Write formatted text to a file PrintWriter
🧵 TL;DR
Category Byte Streams Character Streams Base Classes InputStream
/ OutputStream
Reader
/ Writer
Use When Working with binary data Working with text Examples Image files, network sockets Text files, logs, console