J2SE quick advanced -- four basic abstract classes of IO stream: InputStream, OutputStream, Reader, Writer, and abstract class

Source: Internet
Author: User

J2SE quick advanced -- four basic abstract classes of IO stream: InputStream, OutputStream, Reader, Writer, and abstract class

The above section briefly describes the four abstract classes in the java. io package and Their Relationships. Here we will further summarize them and their subclasses.

At the end of the previous blog, I posted the entire IO family in Java, and then I modified it again. On the basis of this, I identified the node stream and processing stream (Green is the node stream, yellow indicates the processing stream ).

Here we will talk about these four abstract classes respectively.

If you are careful, you may find that most of the input and output streams are in pairs. If there is input, there will be outputs, which correspond one to one.


InputStream

The InputStream function of the input byte stream is to mark the classes that generate input from different data origins.

The basic methods are as follows:

Int read () method: reads a byte and returns the value in the form of an INTEGER (0 ~ 255). If-1 is returned, it is at the end of the input stream;

Int read (byte [] B) method: reads a series of bytes and stores them in array B. The actual number of bytes read is returned. If the data has reached the end of the input stream before reading the data, -1 is returned;

Int read (byte [] B, int off, int len) method: read up to len bytes and store them in a byte array B. The actual number of bytes read is returned, -1 is returned if it has reached the end of the input stream before reading;

Void close () method: close the input stream and release system resources related to the stream.

Long skip (long n) method: skip n Bytes and skip n Bytes, then return the number of bytes actually skipped.

Need to defineInputStreamSubclass applications must always provide methods to return the next input byte.


OutputStream

Output byte stream OutputStream accepts output bytes and sends these bytes to a receiver.

The basic methods are as follows:

Void write (int B) method: write the specified byte to this output stream.

Void write (byte [] B) method: write B. length bytes from the specified byte array to this output stream.

Void write (byte [] B, int off, int len) method: write the len bytes starting from offset off in the specified byte array to this output stream.

Void close () method: close the output stream and release system resources related to the stream

Void flush () method: refresh the output stream and forcibly write all buffered output bytes.

Need to defineOutputStreamSubclass applications must always provide at least one method to write an output byte.


Reader

The input Reader defines how to read data in characters.

The basic methods are as follows:

Int read () method: read a character and return it as an integer (0 ~ 255). If it has reached the end of the input stream,-1 is returned.

Int read (char [] cbuf) method: read a series of characters and store them in array B. The actual number of characters read is returned. If the read has reached the end of the input stream, -1 is returned;

Int read (char [] cbuf, int off, int len) method: read up to len characters and store them in an array cbuf. The actual number of bytes read is returned, -1 is returned if it has reached the end of the input stream before reading;

Void close () method: close the input stream and release system resources related to the stream.

Long skip (long n) method: skip n characters, skip n Bytes, and return the number of characters actually skipped.

The sub-classes must implement only read (char [], int, int) and close () methods (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functions.


Writer

The output Reader defines the method for writing data in characters.

The basic methods are as follows:

Void write (int c) Method: write a character c to the output stream.

Void write (char [] cbuf) method: write a character array cbuf to the output stream.

Void write (String str) method: write the String str to the output stream.

Void write (char [] cbuf, int off, int len) method: write the len characters starting from offset off in the specified String Array cbuf to this output stream.

Void write (String str, int off, int len) method: writes the len characters starting from offset off in the specified String to the output stream.

The sub-classes must implement only write (char [], int, int), flush (), and close () methods (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functions.

 

Although the four abstract classes define the corresponding methods, when reading and writing data, all the classes actually use their subclasses. The subclass usually redefines the relevant methods. Some basic and common classes will be illustrated in the following examples.



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.