Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The role of FileReader and BufferedReader - why wrapp FileReader?

I cannot really grasp what the purpose is for the FileReader and BufferedReader classes in Java.

At docs.oracle one is recommended to wrap a buffered reader around a FileReader object because it's not efficient to use the FileReader directly. Where does the cost or overhead come from?

Let say I have a text file that I want to read into my java program using these classes:

I use FileReader and BufferedReader

FileReader fileReader = new FileReader(new File("text.txt)"); // probably correct???
BufferedReader bufferedReader = new BufferedReader(fileReader);

1) What is the task of the FileReader object here? Is it responsible to make an I/O-request via the OS to the file, and thereafter read bytes? What is the cost with this? Is it true that the FileReader makes several I/O-requests? Or is the cost when the FileReader object has to converting bytes to characters, character by character?

2) The task of the BufferedReader-object - to refer to the last sentence above. - Is the role for the BufferedReader-object to simply buffer arrays of incoming bytes and THEN convert those to character?

Very grateful for answers

Edit: first of all thanks for incoming answers. But I should have mentioned that its exactly this documentation I have studied. Call me stupid or something - but what is meant by "each read request". WHEN is each read request made? how often?

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

This is mostly why a launched this question - It sounds that the FileReader causes a lot of I/O-request which slows erverthing down.

like image 414
user3155478 Avatar asked Aug 01 '14 14:08

user3155478


People also ask

What is FileReader and BufferedReader?

FileReader is used to read a file from a disk drive whereas BufferedReader is not bound to only reading files. It can be used to read data from any character stream.

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.

What is the use of FileReader?

The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.

What is 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.


2 Answers

From oracle docs :

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

So, as the document clearly suggests, Wrapping a BufferedReader around a FileReader prevents reading of data from the file over and over again. BufferedReader buffers the input.

like image 95
TheLostMind Avatar answered Oct 23 '22 06:10

TheLostMind


Java has many I/O classes which may be combined. So you might see something like:

BufferedReader in = new BufferedReader(new InputStreamReader(
    new FileInputStream(new File("..."), "UTF-8"));
...
in.close(); // Closes all.

This allows flexible combination. So an XML parser could use Reader, and not care where the text comes from: file, URL, memory. This as opposed to "simpler" languages, where there is no choice of implementation (Map with implementations HashMap, TreeMap).

Now, FileReader and FileWriter are old utility classes to read from and write to a file in the default operating system encoding. So for local files. Not portable (!) to other operating systems, or requiring a fixed encoding.

For this FileReader extends InputStreamReader which reads binary data (an InputStream) using a FileInputStream. It does just that.

However it makes sense to use a larger memory buffer to read, hence the advice; which also is long-standing. Remember performance was an issue in the early times of java. The only advantage of FileReader-standalone would be for tight memory situations, maybe on a smart phone.

like image 44
Joop Eggen Avatar answered Oct 23 '22 06:10

Joop Eggen