Modify the behavior of the data flow

Source: Internet
Author: User
Tags abstract readline

In Java 1.0, data flow adapts to specific requirements through FilterInputStream and Filteroutputstream's "adorner" (decorator) subclasses. The Java 1.1 io stream follows this idea, but does not continue to use all adorners derived from the same "filter" base class. This can be a little confusing if you understand it by looking at the hierarchy of classes.
In the following table, the corresponding relationship is more rough than the previous one. This difference is caused by the organization of the class: Although Bufferedoutputstream is a subclass of Filteroutputstream, BufferedWriter is not a subclass of Filterwriter (for the latter, Although it is an abstract class, but does not have its own subclasses or approximate subclasses of things, nor a "placeholder" available, so do not bother to look for. However, the interfaces of the two classes are very similar, and in any case, it is obvious that the new version should be used as much as possible, rather than the old version (that is, unless a stream must be generated in some classes, no reader or writer can be generated).

Filters:
Java 1.0 Class

Corresponding Java 1.1 class

FilterInputStream

FilterReader

Filteroutputstream

Filterwriter (abstract class with no subclasses)

Bufferedinputstream

BufferedReader
(also has readLine ())

Bufferedoutputstream

BufferedWriter

DataInputStream

Use datainputstream
(Except When you need the use of readLine (), when your should use a bufferedreader)

PrintStream

PrintWriter

Linenumberinputstream

LineNumberReader

Streamtokenizer

Streamtokenizer
(use constructor that takes a Reader instead)

Pushbackinputstream

Pushbackreader


Filters: Java 1.0 class corresponding Java 1.1 class

FilterInputStream FilterReader
Filteroutputstream Filterwriter (abstract class without subclasses)
Bufferedinputstream BufferedReader (also ReadLine ())
Bufferedoutputstream BufferedWriter
DataInputStream use DataInputStream (unless you want to use ReadLine (), then you need to use a bufferedreader)
PrintStream PrintWriter
Linenumberinputstream LineNumberReader
Streamtokenizer Streamtokenizer (replace reader with builder)
Pushbackinputstream Pushbackreader

One rule is obvious: if you want to use ReadLine (), don't implement it with a datainputstream (or you'll get an error message at compile time), instead use a bufferedreader. But in addition to this, DataInputStream is still the "preferred" member of the Java 1.1 IO library.
To make the transition to printwriter more natural, it provides a builder that can take any OutputStream object. PrintWriter provides less formatting support than PrintStream, but the interfaces are almost the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.