Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specific difference between bufferedreader and filereader

I would like to know the specific difference between BufferedReader and FileReader.

I do know that BufferedReader is much more efficient as opposed to FileReader, but can someone please explain why (specifically and in detail)? Thanks.

like image 997
Outlier Avatar asked Mar 10 '12 17:03

Outlier


People also ask

What is the difference between FileReader and Scanner?

Well: FileReader is just a Reader which reads a file, using the platform-default encoding (urgh) BufferedReader is a wrapper around another Reader , adding buffering and the ability to read a line at a time. Scanner reads from a variety of different sources, but is typically used for interactive input.

What is difference between BufferedReader and InputStreamReader?

BufferedReader reads a couple of characters from the Input Stream and stores them in a buffer. InputStreamReader reads only one character from the input stream and the remaining characters still remain in the streams hence There is no buffer in this case.

What is the difference between FileReader and FileInputStream?

FileInputStream is Byte Based, it can be used to read bytes. FileReader is Character Based, it can be used to read characters. FileInputStream is used for reading binary files. FileReader is used for reading text files in platform default encoding.

Why is BufferedReader more efficient than FileReader for reading a file?

A BufferedReader can be wrapped around a Reader to buffer operations, so instead of 1 byte per call, it reads a bunch at once, thereby reducing system calls and improving performance in most cases. For files: A FileInputStream is the most basic way to read data from files.


2 Answers

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.

like image 117
Mehul Katpara Avatar answered Oct 06 '22 11:10

Mehul Katpara


In simple manner:

A FileReader class is a general tool to read in characters from a File. The BufferedReader class can wrap around Readers, like FileReader, to buffer the input and improve efficiency. So you wouldn't use one over the other, but both at the same time by passing the FileReader object to the BufferedReader constructor.

Very Detail

FileReader is used for input of character data from a disk file. The input file can be an ordinary ASCII, one byte per character text file. A Reader stream automatically translates the characters from the disk file format into the internal char format. The characters in the input file might be from other alphabets supported by the UTF format, in which case there will be up to three bytes per character. In this case, also, characters from the file are translated into char format.

enter image description here

As with output, it is good practice to use a buffer to improve efficiency. Use BufferedReader for this. This is the same class we've been using for keyboard input. These lines should look familiar:

BufferedReader stdin =     new BufferedReader(new InputStreamReader( System.in )); 

These lines create a BufferedReader, but connect it to an input stream from the keyboard, not to a file.

Source: http://www.oopweb.com/Java/Documents/JavaNotes/Volume/chap84/ch84_3.html

like image 30
Mr. Black Avatar answered Oct 06 '22 12:10

Mr. Black