Specific difference between bufferedreader and filereader

First, You should understand “streaming” in Java because all “Readers” in Java are built upon this concept.

File Streaming

File streaming is carried out by the FileInputStream object in Java.

// it reads a byte at a time and stores into the 'byt' variable
int byt;
while((byt = fileInputStream.read()) != -1) {
    fileOutputStream.write(byt);
} 

This object reads a byte(8-bits) at a time and writes it to the given file.

A practical useful application of it would be to work with raw binary/data files, such as images or audio files (use AudioInputStream instead of FileInputStream for audio files).
On the other hand, it is very inconvenient and slower for text files, because of looping through a byte at a time, then do some processing and store the processed byte back is tedious and time-consuming.

You also need to provide the character set of the text file, i.e if the characters are in Latin or Chinese, etc. Otherwise, the program would decode and encode 8-bits at a time and you’d see weird chars printed on the screen or written in the output file (if a char is more than 1 byte long, i.e. non-ASCII characters).

File Reading

This is just a fancy way of saying “File streaming” with inclusive charset support (i.e no need to define the charset, like earlier).

The FileReader class is specifically designed to deal with the text files.
As you’ve seen earlier, the file streaming is best to deal with raw binary data, but for the sake of text, it is not so efficient.

So the Java-dudes added the FileReader class, to deal specifically with the text files. It reads 2 bytes (or 4 bytes, depends on the charset) at a time. A remarkably huge improvement over the preceding FileInputStream!!

so the streaming operation is like this,

int c;
while ( (c = fileReader.read()) != -1) { // some logic }

Please note, Both classes use an integer variable to store the value retrieved from the input file (so every char is converted into an integer while fetching and back to the char while storing).

The only advantage here is that this class deals only with text files, so you don’t have to specify the charset and a few other properties. It provides an out-of-the-box solution, for most of the text files processing cases. It also supports internationalization and localization.

But again it’s still very slow (Imaging reading 2 bytes at a time and looping through it!).

Buffering streams

To tackle the problem of continuous looping over a byte or 2. The Java-dudes added another spectacular functionality. “To create a buffer of data, before processing.”

The concept is pretty much alike when a user streams a video on YouTube. A video is buffered before playing, to provide flawless video watching experience. (Tho, the browser keeps buffering until the whole video is buffered ahead of time.) The same technique is used by the BufferedReader class.

A BufferedReader object takes a FileReader object as an input which contains all the necessary information about the text file that needs to be read. (such as the file path and charset.)

BufferedReader br = new BufferedReader( new FileReader("example.txt") );

When the “read” instruction is given to the BufferedReader object, it uses the FileReader object to read the data from the file. When an instruction is given, the FileReader object reads 2 (or 4) bytes at a time and returns the data to the BufferedReader and the reader keeps doing that until it hits ‘\n’ or ‘\r\n’ (The end of the line symbol).
Once a line is buffered, the reader waits patiently, until the instruction to buffer the next line is given.

Meanwhile, The BufferReader object creates a special memory place (On the RAM), called “Buffer”, and stores all the fetched data from the FileReader object.

// this variable points to the buffered line
String line;

// Keep buffering the lines and print it.
while ((line = br.readLine()) != null) {
    printWriter.println(line);
}

Now here, instead of reading 2 bytes at a time, a whole line is fetched and stored in the RAM somewhere, and when you are done with processing the data, you can store the whole line back to the hard disk. So it makes the process run way faster than doing 2 bytes a time.

But again, why do we need to pass FileReader object to the BufferReader? Can’t we just say “buffer this file” and the BufferReader would take care of the rest? wouldn’t that be sweet?

Well, the BufferReader class is created in a way that it only knows how to create a buffer and to store incoming data. It is irrelevant to the object from where the data is coming. So the same object can be used for many other input streams than just text files.

So being said that, When you provide the FileReader object as an input, it buffers the file, the same way if you provide the InputStreamReader as an object, it buffers the Terminal/Console input data until it hits a newline symbol. such as,

// Object that reads console inputs
InputStreamReader console = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(console);
System.out.println(br.readLine());

This way, you can read (or buffer) multiple streams with the same BufferReader class, such as text files, consoles, printers, networking data etc, and all you have to remember is,

 bufferedReader.readLine();

to print whatever you’ve buffered.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)