Java IO Demystified

Many Java developers feel the Java IO features are confusing, and overwhelming. I felt the same when I first used the Java IO packages. Gradually, I have tried to work my way of the mysteries. Following summary is a bit over general, but has helped me to understand the Java IO packages.

The Basics

The java.io package contains a class for random access files, but mainly, the classes in the package are for handling data-stream. There are two main types of data-streams: byte stream and character stream. There are various sources or destinations that the stream goes in and out. Some of the common source or destinations are file, string, threads, and serialize objects.

Naming

For byte streams, the classes have the names related to InputStream and OutputStream; for character streams, the classes have the names related to Reader and Writer.

The Reader and Writer classes are really just byte streams with additional conversion from bytes to characters, with enhanced support on character encoding.

Subclasses are usually named for one of the following stream-data attributes:

  • the source/destination
  • the types
  • additional processing or filtering performed (operations)

The operations are accomplished through stream filters, which accept compatible object as a parameter to the constructor and manipulate/decorate the input stream with additional processing. These filters are created by extending the base filter classes, FilterInputStream, FilterOutputStream, FilterReader, or FilterWriter.

Examples of the naming:

Operations Source/Destination Stream types Classes names
read/write byte array (byte[]) byte ByteArrayInputStream, ByteArrayOutputStream
read/write char array (char[]) char CharArrayReader, CharArrayWriter
read/write file byte, char FileInputStream, FileOutputStream, FileReader, FileWriter
read/write string (StringBuffer) char StringReader, StringWriter
read/write thread (piped) byte, char PipedInputStream, PipedOutputStream, PipedReader, PipedWriter
buffering compatible stream byte, char BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter
read/write primitive types compatible stream byte DataInputStream, DataOutputStream
object serialization (read/write objects) compatible stream byte ObjectInputStream, ObjectOutputStream
Advertisement
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.