I need to understand the difference between these two classes and how they work with each other. I understand that FileReader reads characters from a file one character at a time and the BufferedReader reads a large chunk of data and stores it in a buffer and thus makes it faster.
In order to use a BufferedReader, i have to provide it a FileReader. How does the BufferedReader class use the FileReader if it reads the file differently? Does it mean that BufferedReader uses FileReader and therefore behind the scenes the characters are still read one character at a time? I guess my question is how does the BufferedReader class use the FileReader class.
First of all,
BufferedReadertakes aReader, not aFileReader(although the latter is accepted).The
Readerabstract class has severalread()methods. There is a read-one-character version as well as two versions that read a block of characters into an array.It only makes sense to use
BufferedReaderif you’re reading single characters or small blocks at a time.Consider the following two requests:
The first one will go to the underlying file, whereas the second one will most probably be satisfied from the
BufferedReader‘s internal buffer.