Understanding Java IO Streams: Concepts, Class Hierarchy, and Usage
This article explains the concept of streams, classifies Java IO streams into character/byte and input/output types, details the main InputStream and OutputStream class hierarchies, and describes related classes such as Reader, Writer, File, and RandomAccessFile for effective data handling.
Concept and Purpose of Streams
A stream is an ordered collection of bytes with a start and end, representing data transmission between devices; it abstracts data transfer to simplify operations.
Classification of IO Streams
IO streams are divided by data type (character streams vs. byte streams) and by direction (input streams vs. output streams).
Character Streams and Byte Streams
Character streams operate on characters using a charset table, while byte streams work with raw bytes; character streams are preferred for pure text, whereas byte streams handle all data types such as images or videos.
Input Streams and Output Streams
Input streams support read operations only, and output streams support write operations only; the choice depends on the nature of the data being transferred.
Java IO Stream Objects
Input Byte Streams (InputStream)
InputStream is the abstract parent of all input byte streams. Basic concrete streams include ByteArrayInputStream, StringBufferInputStream, and FileInputStream, which read from a byte array, a StringBuffer, and a file respectively. PipedInputStream reads from a shared pipe. ObjectInputStream and FilterInputStream subclasses act as decorator streams.
Output Byte Streams (OutputStream)
OutputStream is the abstract parent of all output byte streams. Basic concrete streams include ByteArrayOutputStream and FileOutputStream, which write to a byte array and a file respectively. PipedOutputStream writes to a shared pipe. ObjectOutputStream and FilterOutputStream subclasses serve as decorator streams.
Correspondence Between Byte Input and Output Streams
The diagram shows a largely symmetric relationship between Java byte streams, with some asymmetrical classes such as LineNumberInputStream (no direct output counterpart) and others that could be removed without breaking the package structure.
Character Input Streams (Reader)
Reader is the abstract parent of all character input streams. Concrete streams include CharReader, StringReader, and PipedReader. BufferedReader and its subclasses act as decorators, while FilterReader is the base for custom decorator streams like PushbackReader. InputStreamReader bridges byte streams to character streams, and FileReader connects FileInputStream to a Reader.
Character Output Streams (Writer)
Writer is the abstract parent of all character output streams. Concrete streams include CharArrayWriter, StringWriter, and PipedWriter. BufferedWriter provides buffering, PrintWriter mirrors PrintStream functionality, and OutputStreamWriter bridges OutputStream to Writer, with FileWriter as a common implementation.
Correspondence Between Character Input and Output Streams
Conversion Streams Between Byte and Character Streams
Conversion streams act as bridges: InputStreamReader converts bytes to characters, and OutputStreamWriter converts characters to bytes, useful when encoding or decoding data.
File Class
The File class encapsulates file system objects, providing methods to query metadata, list directories, create, delete, and manipulate files and directories.
RandomAccessFile Class
RandomAccessFile is not part of the stream hierarchy; it wraps a byte stream with an internal buffer and a file pointer, allowing both read and write operations on a file. It creates the file if it does not exist and overwrites from the start if no position is specified, making it suitable for multi‑threaded download scenarios.
Java Group Leader
WeChat ID: javatuanzhang
Daily Java technical sharing
Long press to recognize QR code
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.