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 );}}}