Three key knowledge points to be mastered in the Java I/O Library

Source: Internet
Author: User

We have been discussing the Java I/O library for a long time, And palesting has just published <preliminary Java I/O summary> series of articles, however, I guess some readers will find the Java I/O Library hard to understand, so they will join in and talk about the three key knowledge points I think I should master when learning the Java I/O library.

Knowledge Point 1: four levels of Structure

The Java I/O Library provides four levels: inputstream, outputstream, reader, and writer. Inputstream and outputstream PROCESS 8-bit stream data. Reader and writer process 16-bit bytes stream data. Inputstream and reader process input, and outputstream and writer process output. You must go to the j2se document to check the class inheritance system of these four levels.

In addition to these four series of classes, the I/O Library also provides a few auxiliary classes, among which inputstreamreader and outputstreamwriter are important. Inputstreamreader adapts inputstream to reader, and outputstreamwriter adapts outputstream to writer. In this way, the bridge between byte stream processing class and worker stream processing class is established.

When you use the I/O library, you only need to follow the above rules to find the class you need in the corresponding class system.

Knowledge Point 2: Adaptation

Inheritance in the Java I/O library is not a normal inheritance; its inheritance mode is divided into two types, one is the adapter mode (for detailed analysis, see <> A Book ). The following describes the inputstream class system as an example.

Inputstream has seven direct subclasses: bytearrayinputstream, fileinputstream, pipedinputstream, stringbufferinputstream, filterinputstream, objectinputstream, and sequenceinputstream. The first four adopt the adapter mode. For example, some code of fileinputstream is as follows:

Public class fileinputstream extends inputstream

{

/* File descriptor-handle to the open file */

Private filedescriptor FD;

Public fileinputstream (filedescriptor fdobj)

{

Securitymanager SECURITY = system. getsecuritymanager ();

If (fdobj = NULL ){

Throw new nullpointerexception ();

}

If (Security! = NULL ){

Security. checkread (fdobj );

}

FD = fdobj;

}

// Other code

}

It can be seen that fileinputstream inherits inputstream and combines filedescriptor, which adopts the Object Adapter mode. When learning the I/O library, we should master the adaptation source of the four Object Adapter modes: the adaptation source of bytearrayinputstream is the byte array, and the adaptation source of fileinputstream is the file object, the adaptive source of pipedinputstream is the pipedoutputstream object, and the adaptive source of stringbufferinputstream is a string object. The learning methods for the other three series classes are the same.

Knowledge Point 3: decorator Function

The other three directly sub-classes of inputstream use the decorator mode. <> the description is clear. In fact, we don't have to worry about the mode in which it is used, so we can see the code. The Code of filterinputstream is as follows:

Public class filterinputstream extends inputstream {

/**

* The input stream to be filtered.

*/

Protected inputstream in;

Protected filterinputstream (inputstream in ){

This. In = in;

}

// Other code

}

? Are you sure you want to know? Filterinputstream inherits inputstream and also references inputstream. It has four sub-classes, which are called the decorator mode. We can use the decorator mode for the moment, but now we should know that because inputstream has several other subclasses, we can use other subclasses as parameters to construct the filterinputstream object! This is a common feature in development. The sample code is as follows:

{

// Read the key from the key file

Secretkey key = NULL ;??

Objectinputstream Keyfile = new objectinputstream (

New fileinputstream ("C: // security file // symmetric key // yhb. Des "));

Key = (secretkey) Keyfile. readobject ();

Keyfile. Close ();

}

In the above Code, objectinputstream is also a subclass of inputstream and also uses the decorator function. However, even if you do not understand the decorator mode, you just need to remember the two sections of the filterinputstream code given in this article.

I have mastered the above three points and believe that we can well apply the Java I/O library.

Related Article

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.