J2SE four basic abstract classes of fast advanced--io streams InputStream, OutputStream, Reader, Writer

Source: Internet
Author: User

The four abstract classes and their relationships in the Java.io package are briefly understood above, and further summarize them and their subclasses.

At the end of the previous blog, posted the entire IO family in Java, and then the small and re-modified a bit, on the basis of the previous, identified the node stream and processing stream (green for the node stream, yellow for the processing stream).

Here, let's take a look at these four abstract classes, respectively.

Careful you may find that most of the input and output streams are paired, and there is output for each input and corresponding.


InputStream

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

The basic methods are:

int read () method: Reads a byte, returned as an integer (0~255), if 1 is returned to the end of the input stream;

int read (byte[] b) method: Reads a series of bytes and stores it in an array B, returns the number of bytes actually read, and returns 1 if it has reached the end of the input stream before reading;

int Read (byte[] b,int off,int len) method: reads up to len bytes and stores it in a byte array B, returning the actual number of bytes read, Returns 1 if it has reached the end of the input stream before reading;

void Close () method: closes this input stream and frees the system resources associated with the stream.

long Skip (long N) method: skips n bytes without reading, jumps directly to n bytes, returns the number of bytes actually skipped.

applications that need to define InputStream subclasses must always provide a way to return the next input byte.


OutputStream

The output byte stream outputstream accepts the output bytes and sends the bytes to a sink.

The basic methods are:

void Write (int b) Method: Writes the specified bytes to this output stream.

void Write (byte[] b) Method: Writes B.length bytes from a specified byte array to this output stream.

void Write (byte[] b,int off,int len) method: Writes Len bytes from offset off in the specified byte array to this output stream.

void Close () method: closes this output stream and frees system resources related to this stream

void Flush () method: refreshes this output stream and forces all buffered output bytes to be written out.

applications that need to define OutputStream subclasses must always provide at least one method that can write an output byte.


Reader

the input character Stream reader defines a method for reading data in a character unit.

The basic methods are:

int Read () method: reads a character and returns it as an integer (0~255), or -1 if it has reached the end of the input stream.

int Read (char[] cbuf) method: reads a series of characters and stores them in an array B, returning the actual number of characters read, if it has reached the end of the input stream before reading, then returns-1;

int Read (char[] cbuf, int off,int len) method: Reads up to Len characters and stores it in an array of cbuf, returning the actual number of bytes read, or 1 if the end of the input stream has been reached before reading;

void Close () method: closes this input stream and frees the system resources associated with the stream.

long Skip (long N) method: skips n characters without reading, jumps directly to n bytes, returns the number of characters actually skipped.

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


Writer

The output character Stream reader defines a method for writing data in characters.

The basic methods are:

void Write (int c) method: Writes a character C to the output stream.

void Write (char[] cbuf) method: Writes a character array cbuf to the output stream.

void Write (String str) Method: Writes the string str to the output stream.

void Write (char[] cbuf, int off,int len) method: Writes the Len character in the specified string array cbuf from offset off to this output stream.

void Write (string str, int off,int len) method: Writes the Len character of the specified string str from offset off to this output stream.

subclasses must implement methods that have only write (char[], int, int), flush (), and Close (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functionality.

Although the four abstract classes define the appropriate methods, the subclasses are used to read and write the data, and the subclasses often redefine the relevant methods. Some basic, commonly used classes are illustrated in the following example.



J2SE four basic abstract classes of fast advanced--io streams InputStream, OutputStream, Reader, Writer

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.