Java IO Overview
Jakob Jenkov |
In this text I will try to give you an overview of the classes in the Java IO (java.io
) package.
More specifically, I will try to group the classes after their purpose. This grouping should make
it easier for you in the future, to determine the purpose of a class, or find the class you need
for a specific purpose.
Input and Output - Source and Destination
The terms "input" and "output" can sometimes be a bit confusing. The input of one part of an application
is often the output of another. Is an OutputStream
a stream where output is written to,
or output comes out from (for you to read)? After all, an InputStream
outputs its data to
the reading program, doesn't it? Personally, I found this a bit confusing back in the day when I
first started out learning about Java IO.
In an attempt to clear out this possible confusion, I have tried to put some different names on input and output to try to link them conceptually to where the input comes from, and where the output goes.
Java's IO package mostly concerns itself with the reading of raw data from a source and writing of raw data to a destination. The most typical sources and destinations of data are these:
- Files
- Pipes
- Network Connections
- In-memory Buffers (e.g. arrays)
- System.in, System.out, System.error
The diagram below illustrates the principle of a program reading data from a source and writing it to some destination:
Streams
IO Streams are a core concept in Java IO. A stream is a conceptually endless flow of data. You can either read from a stream or write to a stream. A stream is connected to a data source or a data destination. Streams in Java IO can be either byte based (reading and writing bytes) or character based (reading and writing characters).
The InputStream, OutputStream, Reader and Writer
A program that needs to read data from some source needs an InputStream
or
a Reader. A program that needs to write data to some destination needs an
OutputStream
or a Writer
.
This is also illustrated in the diagram below:
An InputStream
or Reader
is linked to a source of data. An OutputStream
or Writer
is linked to a destination of data.
Java IO Purposes and Features
Java IO contains many subclasses of the InputStream
, OutputStream
, Reader
and Writer
classes. The reason is, that all of these subclasses are addressing various different purposes.
That is why there are so many different classes. The purposes addressed are summarized below:
- File Access
- Network Access
- Internal Memory Buffer Access
- Inter-Thread Communication (Pipes)
- Buffering
- Filtering
- Parsing
- Reading and Writing Text (Readers / Writers)
- Reading and Writing Primitive Data (long, int etc.)
- Reading and Writing Objects
These purposes are nice to know about when reading through the Java IO classes. They make it somewhat easier to understand what the classes are targeting.
Java IO Class Overview Table
Having discussed sources, destinations, input, output and the various IO purposes targeted by the Java IO classes, here is a table listing most (if not all) Java IO classes divided by input, output, being byte based or character based, and any more specific purpose they may be addressing, like buffering, parsing etc.
Byte Based | Character Based | |||
Input | Output | Input | Output | |
Basic | InputStream | OutputStream | Reader InputStreamReader |
Writer OutputStreamWriter |
Arrays | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
Files | FileInputStream RandomAccessFile |
FileOutputStream RandomAccessFile |
FileReader | FileWriter |
Pipes | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
Buffering | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
Filtering | FilterInputStream | FilterOutputStream | FilterReader | FilterWriter |
Parsing | PushbackInputStream StreamTokenizer |
PushbackReader LineNumberReader |
||
Strings | StringReader | StringWriter | ||
Data | DataInputStream | DataOutputStream | ||
Data - Formatted | PrintStream | PrintWriter | ||
Objects | ObjectInputStream | ObjectOutputStream | ||
Utilities | SequenceInputStream |
Tweet | |
Jakob Jenkov |