Reprint please indicate source: http://www.cnblogs.com/lighten/p/7074488.html
1. Preface
As in the order of the byte stream, the character stream also provides a buffer character stream, different from the byte stream, although Java provides the FilterReader and Filterwriter classes, but the buffer character stream does not inherit two classes, but directly inherit the reader and writer class.
2.BufferedReader
The constructor accepts a reader and a number of buffered characters, by default 8192.
The Read method not only uses a synchronous lock, but also takes a for (;;) Form. Nextchar is the character position subscript to be read, Nchars is the number of characters in the current cache. It is clear that the character subscript to be read is greater than or equal to the current character, meaning that the characters in the cache have been read and the write cache needs to be retrieved again. This is the fill () method. After fill () is still not, it means that the reading is finished, return-1. SKIPLF whether to skip the trade-off symbol, false by default. Then it is the character of the CB character array that returns the position of Nextchar, which is increased by 1.
The fill () method needs to take into account the mark generated by the Mark method, marked and unmarked is different, when unmarked is definitely the buffer is used, starting from 0 to re-fill the data. That's the last paragraph. The tagged data cannot be discarded when tagged, and all the values that need to be computed for DST are distinguished by the presence of the Readaheanlimit parameter. This parameter means the maximum number of data that has been read before the token is reset. Therefore, the Fill method calculates the length of the tag bit to the end of the read, if it is greater than this limit, this is an invalid token (marked in the 1th digits, reading to the 5th digits, but only two historical characters readaheanlimit, so that the marker bit is useless, because the extra characters cannot be deleted, So it is an invalid token). Valid tags are divided into two cases, one is readaheanlimit less than the current cache size, this does not matter, the copy from the mark as the beginning of the data can be. The mark is set to 0, and the new data is written from the delta. If it is larger than the cache, it must be re-created.
The method of the read character array is also a common process, first executed once, if it is finished directly back, if not finished, and did not read the number of required to read through the while loop, read the stream directly break, or read the desired Len length.
If there is no readable cache of course to re-fill, but there is also a case where the desired length exceeds the cache size, this time directly from the original stream read back. After re-filling or not, just read, return-1. The next is to fill the data into the given array of characters, the buffer may not be enough data, so the selection of a small value, and then return.
The other methods are not good to say. The last lines method, which is provided by JDK8, is more than the IO category, the function is to return a stream<string>, which is a line of the stream read, lazy loading.
3.BufferedWriter
The output stream Bufferwriter is simpler than the BufferedReader.
Receive a writer and cache size, and the default is 8,192 characters.
Write method, if the cache is full, swipe in to the writer you are holding. Then reset Nextchar to 0 and write to the current location.
I wrote a comparison method, and did not use the Math class method, the reason is also given, the use of the file descriptor, will try to print the stack trace.
The other two write methods are relatively simple, the same way of thinking, will not continue to introduce.
Java IO (11) BufferedReader and BufferedWriter