Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Use the two "R"s of Java 1.1 -- Readers and Writers

Learn how to use the two new additions to the <CODE>java.io</CODE> package -- class <CODE>Reader</CODE> and class <CODE>Writer</CODE> -- to filter out unwanted e-mail

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
If you're joining us for the first time, you might want to begin by reading last month's column, which introduced the concept of streams. The stream model presents information as flowing from one point to another, as if it were in a stream or pipe. The model fits many types of real-world information. Whether it is keycodes coming from a computer keyboard, audio data coming from an audio file, or line after line of text coming from a text file, all appear to be streams of information. It is a simple yet powerful model, and forms the basis for the Java I/O classes.

Java 1.1's two types of streams

The Java 1.1 class library provides two different types of streams -- byte-oriented and character-oriented. I'll explain why in a moment, but first I want to give you some more background.

The two types of streams are organized into two separate class hierarchies, one consisting entirely of the byte-oriented stream classes, and the other consisting entirely of character-oriented stream classes. The classes within the two hierarchies are named consistently, except for their suffix. The byte-oriented stream classes end in either InputStream or OutputStream, while the character-oriented stream classes end in either Reader or Writer. The two hierarchies are functionally almost identical, and they contain most of the same subclass specializations (for example, one contains LineNumberInputStream and the other contains LineNumberReader).

Prior to version 1.1, the Java class library provided only byte-oriented streams. This setup reflected the reality imposed by the operating systems on which Java was developed -- they were hopelessly byte-oriented, and Java was initially tailored with that environment in mind. The byte-oriented stream classes, however, were flawed in one area -- support for Java's highly regarded Unicode character encoding. As you will soon learn, these classes provided little or no support for converting between bytes and characters.

Unicode is an international multi-language character encoding standard. Most operating systems we are familiar with use ASCII (American Standard Code for Information Interchange) encoding. ASCII (with a 7-bit, 128-character set) doesn't do a very good job supporting characters from languages similar to ours (Danish, for example), much less the character sets for languages such as Japanese or Thai. By basing its character encoding on Unicode (with a 16-bit, 65,536-character set) instead of ASCII, Java supposedly solved that problem -- or did it?

Things aren't always what they seem -- PrintStream

Let's take a look at the Java 1.0 implementation of PrintStream, a byte-oriented subclass of OutputStream, and the class from which the objects System.out and System.err are constructed.

The PrintStream class provides a bevy of methods for printing Java primitive data types and objects to an output stream. These are the two most obvious:

public void println(char [] rgc); public void println(String str);

Here are their implementations:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • The Reader class for Java 1.1 http://www.javasoft.com/products/jdk/1.1/docs/api/java.io.Reader.html
  • The Writer class for Java 1.1 http://www.javasoft.com/products/jdk/1.1/docs/api/java.io.Writer.html
  • I/O Enhancements for Java 1.1 http://www.javasoft.com/products/jdk/1.1/docs/guide/io/index.html
  • Previous How-To Java articles