The read () method in System. in, system. inread
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. Iflen
If 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
In eclipse, which of the following parameters is input after you run the program using the Systeminread () method?
In the bottom console, It is the output window.
In java, why does SysteminRead () write like this?
You have not found it comprehensive enough. You should check what System. in is, and check whether there are other classes with the read () method?
----------------------------------
After checking, you will know. I can tell you that System. in is InputStream and there is also the read method below.
Then you can see that InputStream is actually an abstract class. However, this does not mean that in cannot be a subclass of InputStream or an internal implementation class. If it is an internal implementation class, needless to say, it can be instantiated naturally. If it is a subclass, it is to define in with polymorphism, such
Public static InputStream in = new MyInputStream ();
As long as MyInputStream is an entity class and a subclass of InputStream.
------------------------------------
Has sh2619978 read the source code? I have never seen it, but it makes sense to say that it is an internal static class, because even if it is a subclass, It is externally invisible and it is a System-specific InputStream object. The first reflection is internal.