Read () method in system. In

Source: Internet
Author: User

Let's take a look at the following program.

public class TestInputStream {public static void main(String args[]) throws IOException {InputStream in = System.in;int a = in.read();System.out.println(a);a = in.read();System.out.println(a);a = in.read();System.out.println(a);}}
Question 1: What Will be output if "A" is input?

In fact, when I input "A", it does not respond.

Question 2: If I enter "a" and press enter, what will be output?

97 13 10, and the program stops running

Question 3: What Will be output if you press enter?

13 10, and the program is not running

Question 4: What Will be output if "ABC" is output?

97 98 99

Now, the example is complete. Let's analyze the above four cases.

First, we need to know what system. In is?

Directly output system. In. We can find that it is a bufferedinputstream.

So how does bufferedinputstream. Read () work?

We can roughly scan the source code.

Private void fill () throws ioexception {byte [] buffer = getbucket open (); If (markpos <0) Pos = 0;/* No mark: throw away the buffer */else if (Pos> = buffer. length) {/* no room left in buffer * // here is the operation after Mark is set, no concern} COUNT = Pos; int n = getinifopen (). read (buffer, POs, buffer. length-Pos); If (n> 0) Count = N + Pos ;}

Buffer is the buffer of bufferedinputstream. The read (byte [] B, int off, int Len) method reads data from the underlying input stream. This method is a blocking method, so when will it be returned?

After debugging, we found that the underlying input stream in system. In is fileinputstream. According to the description in the official documentation:

1. IflenIf the value is not 0, the method is blocked before the input is available.

2. Return the total number of bytes read into the buffer. If there is no more data at the end of the file-1.

In this case, how can I determine that the input in the console reaches the "end of the file? Press enter to submit the data and tell the program that the data has been submitted.


The following are one-to-one answers to the above questions:

1. When you enter data in the console, pressing the Enter key means that the input is complete and the entered content will be submitted to the read () method. Therefore, before pressing the Enter key, the program does not know what you entered.

2. Enter "a" and press Enter. At this time, the read () method reads data. In this case, the data in the buffer should be 97 13 10, that is, the character 'a ', '\ R',' \ n', And the read () method can only read one character from the buffer at a time, and the three characters are exactly read after three read, then the program ends.

3. Press enter only, and read-only "\ r" and "\ n" from the stream. because there is not enough input, it is blocked when read () is called for the third time.

4. Input "ABC", and read "A", "B", "C", "\ r", "\ n" from the stream. Because only read () 3 times, then read ABC, \ r, and \ n in buffer






Read () method in system. In

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.