HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

Most programs use data in one form or another - as input, output or both. The sources of input and output can vary from a local file to a socket on the network, a database, in memory or another program. Even the type of data can vary from objects and characters to multimedia and more.

The APIs Java provides for reading and writing streams of data have been part of the core Java Development Kit since version 1.0, but they're often overshadowed by the better known JavaBeans, JFC, RMI, JDBC and others. However, input and output streams are the backbone of the Java APIs, and understanding them is not only crucial but can also make programming with them a lot of fun.

In this article we'll cover the fundamentals of I/O streams by looking at the various stream classes and covering the concept of stream chaining. Next month we'll look at some example uses of I/O streams.

To bring data into a program, a Java program opens a stream to a data source - such as a file or remote socket - and reads the information serially. On the flip side a program can open a stream to a data source and write to it in serial fashion. Whether you're reading from a file or from a socket, the concept of serially reading from, and writing to, different data sources is the same. For that very reason, once you understand the top-level classes (java.io.Reader, java.io.Writer), the remaining classes are a breeze to work with.

Character versus Byte Streams
Prior to JDK 1.1, the input and output classes (mostly found in the java.io package) supported only 8-bit "byte" streams. In JDK 1.1 the concept of 16-bit Unicode "character" streams was introduced. While the byte streams were supported via the java.io.InputStream and java.io.OutputStream classes and their subclasses, character streams are implemented by the java.io.Reader and java.io.Writer classes and their subclasses.

Most of the functionality available for byte streams is also provided for character streams. The methods for character streams generally accept parameters of data type "char" parameters, while "byte" streams - you guessed it - work with "byte" data types. The names of the methods in both sets of classes are almost identical except for the suffix; that is, character-stream classes end with the suffix Reader or Writer and byte-stream classes end with the suffix InputStream and OutputStream. For example, to read files using character streams, you'd use the java.io.FileReader class; to read using byte streams you'd use java.io.FileInputStream.

Unless you're working with binary data such as image and sound files, you should use readers and writers to read and write information for the following three reasons:

  1. They can handle any character in the Unicode character set (the byte streams are limited to ISO-Latin-18-bit bytes).
  2. Programs that use character streams are easier to internationalize because they're not dependent upon a specific character encoding.
  3. Character streams use buffering techniques internally and are therefore potentially much more efficient than byte streams.
To bridge the gap between the byte and character-stream classes, Java provides the java.io.InputStreamReader and java.io.OutputStreamWriter classes. The only purpose of these classes is to convert byte data into character-based data according to a specified (or the platform default) encoding. For example, the static data member "in" in the "System" class is essentially a handle to the Standard Input (stdin) device. If you wanted to "wrap" this inside the java.io.BufferedReader class that works with character streams, you'd use InputStreamReader class as follows:

BufferedReader in = new BufferedReader(new

For JDK 1.0 Versions
If you can't use JDK 1.1, perhaps because you're developing applets for older browsers, simply use the byte-stream versions, which work just as well. Although I haven't discussed these versions much, they work almost identically to the character versions from a developer's perspective except, of course, the reader/writers accept character data types versus byte data types.

The Various Stream Classes
Top-Level Classes: java.io.Reader and java.io.Writer
Reader and Writer are the abstract parent classes for character stream-based classes in the java.io package. As discussed above, Reader classes are used to read 16-bit character streams and Writer classes are used to write to 16-bit character streams. The methods for reading and writing to streams found in these and their descendant classes (discussed in the next section) are:

int read()
int read(char cbuf[])
int read(char cbuf[], int offset, int length)
int write(int c)
int write(char cbuf[])
int write(char cbuf[], int offset, int length)

Listing 1 demonstrates how the read and write methods can be used. The program is similar to the MS-DOS type and Unix cat commands, that is, it displays the contents of a file. The following code fragment from Listing 1 opens the input and output streams:

FileReader fr = new FileReader(args[0]);
PrintWriter pw = new PrintWriter(System.out, true);

The program then reads the input file and displays its contents till it hits an end of file condition (-1), as shown here:

while ((read = fr.read(c)) != -1)
pw.write(c, 0, read);

I used the "(char cbuf[])" version of the read method, because reading a single character at a time can be approximately five times slower than reading chunks (array) at a time.

Other notable methods in the top level classes include skip(int), mark(int), reset(), available(), ready() and flush().

  • skip(), as the name implies, allows you to skip over characters.
  • mark() and reset() provide a bookmarking feature that allows you to read ahead in a stream to inspect the upcoming data but not necessarily process it. Not all streams support "marking". To determine whether a stream supports it, use the markSupported() method.
  • InputStream.available() tells you how many bytes are available to be read before the next read() will block. Reader.ready() is similar to the available() method, except it does not indicate how many characters are available.
  • The flush() method simply writes out any buffered characters (or bytes) to the destination (e.g., file, socket).
Specialized Descendant Stream Classes
Several specialized stream classes sub-class from the Reader and Writer classes to provide additional functionality. For example, the BufferedReader provides not only buffered reading for efficiency but also methods such as "readLine()" to read a line of input.

The class hierarchy shown in Listing 4 portrays a few of the specialized classes found in the java.io package. This hierarchy merely demonstrates how stream classes extend their parent classes (e.g.. LineNumberReader) to add more specialized functionality. Tables 1, 2 and 3 provide a more comprehensive list of the various descendant classes found in the java.io and other packages, along with a brief description for each class.

Table 1 & 2 & 3

These descendant classes are divided into two categories: those that read from or write to "data sinks", and those that perform some sort of processing on the data (this distinction is merely to group the classes into two logical sections; you don't have to know one way or the other when using them).

Listings 2 and 3 don't contain the complete list for the table because I intentionally skipped the "byte" counterparts to the "char" based classes and a few others (please refer to the JDK API reference guide for a complete list).

Stream Chaining
One of the best features of the I/O stream classes is that they're designed to work together via stream chaining.

Stream chaining is the concept of "connecting" several stream classes together to get the data in the form required. Each class performs a specific task on the data and forwards it to the next class in the chain. Stream chaining can be very handy. For example, in our own 100% Pure Java backup software, BackOnline, we chain several stream classes to compress, encrypt, transmit, receive and finally store the data in a remote file.

Figure 1 portrays chaining of three classes to convert raw data into compressed and encrypted data, which is stored in a local file. The data is written to GZIPOutputStream, which compresses the input data and sends it to CryptOutputStream. CryptOutputStream encrypts the data prior to forwarding it to FileOutputStream, which writes it out to a file. The result is a file that contains encrypted and compressed data.

Figure 1
Figure 1:

The source for the stream chaining shown in Figure 1 would look something like the code seen here:

FileOutputStream fos = new FileOutputStream("myfile.out");
CryptOutputStream cos = new CryptOutputStream(fos);
GZIPOutputStream gos = new GZIPOutputStream(bos);

or simply:

GZIPOutputStream gos = new
GZIPOutputStream(new CryptOutputStream
(new FileOutputStream("myfile.out")));

To write to chained streams, simply call the write() method on the outermost class as shown here:


Similarily, when closing chained streams, you need only to close the outermost stream class since the close() call is automatically trickled through all the chained classes; in our example above we would simply call the close() method on the GZIPOutputStream class.

In this article we reviewed the basic concepts of Java's I/O streams, which should give you a good understanding of how to program with them. Be sure to tune in next month when we'll complete this article by looking at lots of source code to get a feel for the various uses of I/O streams such as files, databases, sockets, archives and much more.

About the Author
Anil Hemrajani is a senior consultant at Divya Incorporated, a firm specializing in Java/Internet solutions. He provides consulting services to Fortune 500 companies and is a frequent writer and speaker. He can be reached at [email protected]


Listing 1: Type.java.
import java.io.*; 

/** Displays contents of a file (e.g. java Type app.ini) */ 
public class Type 
    public static void main(String args[]) 
                       throws Exception 
       // Open input/output and setup variables 
       FileReader  fr = new FileReader(args[0]); 
       PrintWriter pw = new PrintWriter(System.out, true); 
       char c[]       = new char[4096]; 
       int  read      = 0; 

       // Read (and print) till end of file 
       while ((read = fr.read(c)) != -1) 
          pw.write(c, 0, read); 

       // Close shop 


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.