Java io (i) InputStream and OutputStream

Source: Internet
Author: User

Reprint please indicate source: http://www.cnblogs.com/lighten/p/6964702.html

1. Preface

The IO operation of the computer has always been an important one, and IO as the name implies, is the input and output stream. Whether it is disk IO or network IO, data flow can not be separated from Io, understand Java IO Operation class is very important, this article introduces the abstract parent of Io InputStream and OutputStream, which is the basis of Java IO operation, understand the abstract class, It is also helpful for other IO streams.

Streams all implement the Closeable interface, which means that all need to be closed. An additional flushable method is implemented by the output stream, which essentially notifies the data to be immediately brushed into the specified output.

2.InputStream

The 1.read () method is an abstract method, which is the source of the data read, which is used to read the data and return the read value. Subclasses must implement this method.

The use of 2.read (byte[]) method is to read the input of a stream into the byte[] array, which is actually called read (byte[], int, int).

3.read (byte[], int, int), starting at the first position of reading the stream into the byte[] data, reading Len bytes.

The above is read into the whole process of byte array, first determine the correctness of the parameters, and then read a byte, if 1 then return 1 (stream with-1 to determine whether the end), and then put in the array off position, after the loop read, three cases return: 1 read the given data full, 2 read-1 Terminator, 3 A read exception occurred. Finally, the number of data read by the array is returned.

4.skip (long), skipping the specified byte is not read.

The above code is clear, create a skipped buffer data, the stream is read into this, the number of skipped bytes has a length limit of 2048. By looping out the infection of an IO exception until it is read or read full to skip, and then returns the actual number of skipped.

5.available (), which returns the estimated number of readable, the parent class is returned directly to 0. This method is not accurate for all stream implementations, so use caution. This method requires quilt class overwrite. A single read or skip of many bytes is not blocked, but a small number of bytes may be read or skipped.

6.close (), close stream, no specific implementation. will release operating system resources

7.mark (int), mark the read location, if the flow implementation support, you can reset the location, the next read is identical. Nothing is done in the parent class method. Before using the marksupported () method to determine whether support.

8.reset (), reset to the location of the last call to the Mark method. The parent class is more than mark, so it throws an IO exception directly.

9.markSupported (), which is used to interpret whether the current stream supports the tag stream. It is used with the previous two methods.

3.OutputStream

1.write (int), abstract method, usually writes a byte, the lower 8 bits will be written, and the high 24 bits will be ignored. Writes the specified bytes to the output, which must be implemented by subclasses.

2.write (byte[]), is actually called method 3:write (b, 0, b.length);

3.write (byte b[], int off, int len), starting with the real position of byte array B, write bytes of len length

A simple array boundary decision is called by looping through the write (int) method.

4.flush (), forced flush method, writes write-out bytes (not necessarily at this point to the destination such as disk, possibly in the cache), calls the Flush method to force the write cache bytes to the destination. The parent class method has no specific implementation. However, this method does not guarantee that a destination is being reached, such as a disk, a file stream, this method only guarantees that the previously written data to the operating system write method, does not guarantee that the operating system completed writing this step.

5.close () to close the output stream. The stream cannot be output bytes after it is closed, nor can it be opened again. The parent method is an abstract method.

  

Java io (i) InputStream and OutputStream

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.