Java.Streams.

🔄 Commonalities

FeatureInputStream / OutputStreamReader / Writer
Belong tojava.io packagejava.io package
Abstract classesYes ✅Yes ✅
Base for stream hierarchyYes ✅Yes ✅
Support chaining/wrappingYes ✅Yes ✅ (e.g., with buffering)
Need to be closedYes ✅ (implements Closeable)Yes ✅ (implements Closeable)
Support try-with-resourcesYes ✅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

FeatureInputStreamOutputStreamReaderWriter
📦 TypeByte-basedByte-basedCharacter-basedCharacter-based
🔁 DirectionReadWriteReadWrite
🔢 Operates onbyte (8-bit)byte (8-bit)char (16-bit Unicode)char (16-bit Unicode)
🧠 Use CaseRead binary data (e.g. files, images)Write binary dataRead text (files, strings)Write text
🎯 Example ClassesFileInputStream, BufferedInputStreamFileOutputStream, PrintStreamBufferedReader, FileReaderBufferedWriter, 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

TaskBest Option
Read a binary file (e.g. image)InputStream
Write raw bytes to a socketOutputStream
Read lines from a text fileBufferedReader
Write formatted text to a filePrintWriter

🧵 TL;DR

CategoryByte StreamsCharacter Streams
Base ClassesInputStream / OutputStreamReader / Writer
Use WhenWorking with binary dataWorking with text
ExamplesImage files, network socketsText files, logs, console
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.