Java IO framework

Source: Internet
Author: User

Http://blog.csdn.net/licl19870605/archive/2010/05/18/5604327.aspx

 

Java IO framework

It can be discussed from the IO class level and the IO framework design mode.

In general, I/O can be divided into byte streams and byte streams. The difference is that the byte stream is encapsulated by the byte stream. After I/O is read, the byte stream is processed by JVM. The default JVM encoding determines which character encoding is used for the character of the upstreaming stream.
Each stream is divided into Input and Output. In general, Io has four top-level classes:
Inputstream
Outputstream
Reader
Writer

For machine-level direct interaction, byte streams are used:
Inputstream
Outputstream

The intermediate class of Io is used to convert the byte stream to the upstream stream. Generally, it can be used as a construction parameter of the upstream stream class, and encoding can be specified:
Inputstreamreader
Outputstreamwriter

The buffer containers of the byte stream and the byte stream can be divided into byte and char, so they are derived from:
Bytearrayinputstream
Bytearrayoutputstream
Chararrayinputstream
Chararrayoutputstream

The above classes only represent the stream representation, while the transmission form also shows whether there is a buffer. Therefore, subclass can be derived as a buffer class:
Bufferinputstream
Bufferoutputstream
Bufferreader
Bufferwriter

Each top-level class supports directory files:
Fileinputstream
Fileoutputstream
Filereader
Filewriter

From the interface implemented by the structure of the entire class framework, the top-level class:
Inputstream implements closable,
Outputstream implements closable, flushable
Reader implements closable, readable
Writer implements closable, flushable, appendable
Interface methods:
Closable: Close ();
Flushable: flush ();
Readable: Read (charbuffer CB );
Appendable: append ();
From the rules used by Io classes, it is natural to implement these methods and must be implemented. For example, the stream must be closed after use, and the stream must be refreshed before output. Note that none of the four top-level classes implement the serializable interface.
Java. io. although serializable belongs to the I/O framework, it does not act directly on the four top-layer I/O classes of the I/O framework and their subclasses, but rather on other objects so that they can be serialized, then input and output the data through the IO class. Such as file, objectstreamclass, and string.

In Java, a powerful function is to serialize objects and convert them to binary output, that is, byte stream output rather than bytes stream output. Therefore, there are top-level inputstream and outputstream Derived classes:
Objectinputstream
Objectoutputstream

The stream includes the node stream and the filter stream. It is important to note that the stream can be read and written while performing operations on the data, and implements synchronization operations. The top layer filters the stream class:
Filterinputstream
Filteroutputstream
Its subclass encapsulates the node stream based on it. Common subclasses include:
Bufferinputstream bufferoutputstream
Datainputstream dataoutputstream
Linenumberinputstream printstream
Refer to the subclass construction method.

The above I/O operations basically load the stream into the memory and perform sequential read/write operations on it, while random read/write is required:
Randomaccessfile

The Java IO framework mainly applies two design modes: decoration mode and adapter mode.

Let's just divide Java Io into two parts: element and behavior. elements are adaptive, and behavior is decorative and enhanced.

The adapter mode mainly refers to the conversion of byte streams to the primary stream and the packaging of elements, such as: inputstreamreader, chararrayreader, filereader, pipedreader, and stringreader.
The decoration mode is mainly used to enhance the convection, such as buffering, filtering, and row locating, such as bufferedreader, filterreader, and linenumberreader.

a typical example is:
bufferreader BR = new bufferreader (New inputstreamreader (system. in);
two modes are integrated: inputstream is adapted to inputstreamreader, and inputstreamreader is enhanced to decorate bufferedreader.

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.