The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: JDK(TM) 1.1 -- And Beyond!
Lesson: Migrating to 1.1

## Alternatives to Deprecated Classes and Methods in java.io

### Deprecated Classes in java.io

> The following two classes have been deprecated in `java.io`. `LineNumberInputStream` was deprecated because it incorrectly assumes that bytes adequately represent characters. `StringBufferInputStream` was deprecated because it does not properly convert characters into bytes. As of JDK 1.1, the preferred way to operate on character streams is via the new set of character-stream classes which provides character-based alternatives to both `LineNumberInputStream` and `StringBufferInputStream`.

> Deprecated Class | Alternative
> --- | ---
> `LineNumberInputStream` | `LineNumberReader`
> `StringBufferInputStream` | `StringReader`

### The `java.io.ByteArrayOutputStream` Class

> The following method in `java.io.ByteArrayOutputStream` was deprecated because it does not properly convert bytes into characters. As of JDK 1.1, the preferred way to do this is via one of the other two `toString` methods, which allow the caller to specify the character encoding to use to convert bytes to characters or uses the platform's default character encoding.

> Deprecated Method | Alternative
> --- | ---
> `String toString(int hibyte)` | `String toString()` or `String toString(String enc)`

### The `java.io.DataInputStream` Class

> The following method in `java.io.DataInputStream` has been deprecated because it does not properly convert bytes to characters.

> Deprecated Method | Alternative
> --- | ---
> `String readLine()` | `String BufferedReader.readLine()`

> The alternative shown here will not work for all programs. For examples and discussion about other choices, see How to Convert Code that Uses I/O.

### The `java.io.PrintStream` Class

> The `java.io.PrintStream` class has been superceded by the character-based `java.io.PrintWriter` class. To discourage its use all the constructors for `java.io.PrintStream` have been deprecated.

> Deprecated Method | Alternative
> --- | ---
> `PrintStream(OutputStream)` | `PrintWriter(OutputStream)`
> `PrintStream(OutputStream, boolean)` | `PrintWriter(OutputStream, boolean)`

> > Related Notes: The `PrintStream` class has been modified to use the platform's default character encoding and the platform's default line terminator. Thus each `PrintStream` incorporates an `OutputStreamWriter`, and it passes all characters through this writer to produce bytes for output. The `println` methods use the platform's default line terminator, which is defined by the system property `line.separator` and is not necessarily a single newline character (`'\n'`). Bytes and byte arrays written via the existing `write` methods are not passed through the writer.

> > The primary motivation for changing the `PrintStream` class is that it will make `System.out` and `System.err` more useful to people writing Java programs on platforms where the local encoding is something other than ASCII. `PrintStream` is, in other words, provided primarily for use in debugging and for compatibility with existing code. Therefore its two constructors have been deprecated.

> > Deprecating the constructors rather than the entire class allows existing uses of `System.out` and `System.err` to be compiled without generating deprecation warnings. Thus programmers just learning Java, or programmers inserting `System.err.println` calls for debugging purposes, will not be bothered by such warnings. Programmers writing code that explicitly constructs a `PrintStream`, on the other hand, will see a deprecation warning when that code is compiled. Code that produces textual output should use the new `PrintWriter` class, which allows the character encoding to be specified or the default encoding to be accepted. For convenience, the `PrintWriter` class provides constructors that take an `OutputStream` object and create an intermediate `OutputStreamWriter` object that uses the default encoding.

### The `java.io.StreamTokenizer` Class

> The constructor for `java.io.StreamTokenizer` that operates on an `InputStream` has been deprecated in favor of the constructor that operates on a `Reader`. You can still tokenize an `InputStream` by converting it to a Reader:

> >     Reader r = new BufferedReader(new InputStreamReader(is));
> >     StreamTokenizer st = new StreamTokenizer(r);

> Deprecated Method | Alternative
> --- | ---
> `StreamTokenizer(InputStream)` | `StreamTokenizer(Reader)`

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.