The readLine Method for BufferedReader source code analysis, bufferedreader
The readLine method is a very common method in BufferedReader.
With this function, we can read data from a row in an input stream.
To distinguish rows, use "\ r", "\ n", or "\ r \ n". below is the source code of readLine in BufferedReader.
Detailed analysis is written in comments.
String readLine (boolean ignoreLF) throws IOException {StringBuffer s = null; int startChar; synchronized (lock) {// ensure that the underlying inputStream is not disabled ensureOpen (); // ignoreLF is always false in BufferedReader. // skipLF is set to true after reading the "\ r" character, in this way, the "\ n" boolean omitLF = ignoreLF | skipLF; // the next two loops are ignored. The first bufferLoop is mainly filled with characters from the underlying array, the underlying array is equivalent to a buffer zone. The buffer size can be specified by the second parameter of the constructor BufferedReader (Reader in, int // sz). The default value is 8192 bytes. // CharLoop is used to traverse the underlying array. Each time you read the data in the underlying array, the data is written to a StringBuffer, // when "\ r" or "\ n" is read, the result is returned after the last batch of data is written and the entire loop bufferLoop: for (;) is exited (;;) {// nextChar indicates the position of the next character to be read, nChars = number of characters read from inputstream + nextCharif (nextChar> = nChars) fill (); // read the end of inputstream if (nextChar> = nChars) {/* EOF */if (s! = Null & s. length ()> 0) return s. toString (); elsereturn null;} boolean eol = false; char c = 0; int I;/* Skip a leftover '\ n ', if necessary */if (omitLF & (cb [nextChar] = '\ n') nextChar ++; skipLF = false; omitLF = false; // exit this loop in two cases: 1. Read "\ r" or "\ n" 2. The underlying array cb is read charLoop: for (I = nextChar; I <nChars; I ++) {c = cb [I]; if (c = '\ n') | (c =' \ R ')) {eol = true; break charLoop ;}// read the starting position of the underlying array startChar = NextChar; // The position currently read. nextChar = I; // whether the row (end of line) if (eol) {String str; if (s = null) {// simply use s here. Why do you want to create a str variable str = new String (cb, startChar, I-startChar);} else {s. append (cb, startChar, I-startChar); str = s. toString () ;}// the position to be read next time nextChar ++; // if you read "\ n" next time, it will be ignored, instead of terminating read if (c = '\ R') {skipLF = true;} return str;} // write read data if (s = null) s = new StringBuffer (defaultExpectedLineLength); s. append (cb, startChar, I-startChar );}}}
Which of the following statements about the readLine () method of the BufferedReader class is true ()
A) method readLine () reads A row of data each time.
C) This method may throw an IOException. When calling this method, you should usually put it in the try block and handle the exception through the catch Block.
D) if the end of the stream is read, The result returned by this method is null.
Call the readline () method in bufferedreader
If (file. canRead () to if (bread. ready () Try