Usage of read () and read (byte [] B) in InputStream in java

Source: Internet
Author: User

Both methods exist as abstract methods in the abstract class InputStream,

JDK APIs describe the two as follows:

Read (): the next byte that reads data from the input stream. The int byte value between 0 and 255 is returned. -1 is returned if no bytes are available because it has reached the end of the stream. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.

Read (byte [] B): reads a certain number of bytes from the input stream and stores them in the buffer array B. Returns the actual number of bytes read as an integer. This method is always blocked until the input data is available, the end of the file is detected, or an exception is thrown.

If the length of B is 0, no bytes are read and 0 is returned. Otherwise, at least one byte is read. If the stream is at the end of the file and there are no available bytes, the returned value is-1; otherwise, at least one byte is read and stored in B.

Store the first byte read in element B [0], the next byte in B [1], and so on. The maximum number of bytes read is equal to the length of B. Set k to the number of bytes actually read; these bytes are stored in the elements of B [0] to B [k-1], without affecting B [k] to B [B. length-1.

 

According to the explanation in the help document, the read () method can only read one byte at a time, so it can only read some characters within the ASCII code range. These characters are mainly used to display modern English and other Western European languages. Unicode characters such as Chinese characters cannot be read normally. It can only be displayed in garbled format.

The preceding disadvantages of the read () method are solved in read (byte [] B). For example, a Chinese character occupies two bytes, you can define parameter array B as an array of 2 to read Chinese characters. Of course, B can also be defined as larger. For example, if B = new byte [4], two Chinese characters can be read at a time, but note that, if the size of B is defined as an odd number such as 3 or 7, a document that is full of Chinese characters cannot be fully read and written.

The following example demonstrates the usage of the two:

Example Description: use the InputStreamTest. java class to demonstrate the read () method. Class InputStreamTest1.java to demonstrate the use of read (byte [] B. The main task of the two classes is to read the content in the text file yhsf-txt through the file input stream fileinputstreamand output it to the console for display.

First, download the content of the yhsf-txt file:

The InputStreamTest1.java code is as follows:

Running result:

3.1415926 No pains, No gains !!

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.