Difference Between BufferedReader and FileReader in Java
Last Updated :
13 Jun, 2022
BufferedReader and FileReader both classes are used to read data from a given character stream. Both of them have some pros and some cons. In this article, we will discuss the differences between them. Though the most important difference is in the way they work, but we will discuss the other details also.
What is a Buffer?
A buffer is a small portion of the device memory storage used to temporarily store some amount of data. Usually, Buffers use the RAM of the device to store the temporary data, and hence, accessing data from the buffer is much faster than accessing the same amount of data from the hard drive.
Differences between BufferedReader and FileReader are illustrated over and discussed over major parameters taken into consideration as follows:
- Usage
- Efficiency
- Speed
- Reading lines
1. Usage
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. FileReader class provides two constructors:
- FileReader(File file): It takes a File object that represents a file in your disk and creates a new FileReader instance.
- FileReader(FileDescriptor fd) : Creates a new FileReader, given the FileDescriptor to read from.
- FileReader(String fileName): Takes the name of the file as the only parameter and creates a new FileReader instance to read the file.
BufferedReader class provides two constructors:
- BufferedReader(Reader rd): It uses a Reader to read data from the character input stream and creates a default sized input buffer.
- BufferedReader(Reader rd, int size): Takes two parameters:
- First: A Reader that is used to read the input stream data
- Second: The size of the input buffer. It creates a new BufferedReader with the given sized input buffer.
As seen, BufferedReader accepts any type of Reader(StringReader, FileReader, etc.) and hence capable of reading from any character input stream. Whereas, FileReader is capable of reading characters from files only. Usually, we wrap a FileReader with BufferedReader to read characters from files.
2. Efficiency
BufferedReader is much more efficient than FileReader in terms of performance. FileReader directly reads the data from the character stream that originates from a file. Each time, it reads a character, it directly accesses the disk drive and every time it takes some time for the disk drive to position the read head correctly which makes it very inefficient.
Whereas BufferedReader creates an input buffer and allows the input to be read from the hard drive in large chunks of data rather than a byte at a time, resulting in a huge improvement in performance. The default buffer size is 8Kb( which is enough in most cases) though it can be customized. BufferedReader reads lots of data at a time and stores it in the created buffer memory. When java.io.BufferedReader#read() is called, it reads the data from the memory buffer. When data is not available in the buffer, it makes a corresponding read request of the underlying character stream and loads lots of data into the created buffer. As a result, we do not have to access the hard drive directly when reading each character, we can read from the buffer memory which is fast and much more efficient.
3. Speed
As BufferedReader uses buffer internally, this class is much faster than FileReader. BufferReader doesn’t need to access the hard drive every time like FileReader and hence faster.
4. Reading Lines
In most cases, you would like to read a line at a time rather than reading a character at a time and only the BufferedReader provides a readLine() method that reads a whole line at a time. Simply, the given Reader(FileReader in this case) reads the characters and stores them in the buffer. When the java.io.BufferedReader#readLine() method is called, characters of a line stored in the buffer, are returned as a String. It saves lots of time and hence is faster than FileReader#read() method. Note that, BufferedReader is able to read a whole line at a time only because it uses a buffer memory, it can store the characters of a line in the buffer and read all the characters together directly from the buffer.
Conclusion: Differences between BufferedReader and FileReader
Basis |
BufferedReader |
FileReader |
Use |
It is used to read characters from any type of character input stream (String, Files, etc.) |
It can be used only for reading files |
Buffer |
Uses Buffer internally to read characters from |
Doesn’t use Buffer. Directly reads from the file by accessing the hard drive. |
Speed |
Faster |
Slower |
Efficiency |
Much more efficient for reading files |
Less efficient |
Reading Lines |
BufferedReader can be used to read a single character at a time as well as a line at a time. |
It can read only one character at a time, can not read lines |
Implementation:
Example 1: Reading lines using BufferedReader
Java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
FileReader reader;
try {
reader = new FileReader( "geeks.txt" );
BufferedReader buffer
= new BufferedReader(reader, 16384 );
String line = "" ;
long initialTime = System.currentTimeMillis();
while ( true ) {
try {
line = buffer.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
if (line == null )
break ;
System.out.println(line);
}
System.out.println();
System.out.println( "Time taken : "
+ (System.currentTimeMillis()
- initialTime));
try {
buffer.close();
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
|
Output: From the file ‘geeks.txt’ in the local directory used in the above program
Hello geeks!
BufferedReader uses a FileReader to read data from this file.
BufferedReader creates a input buffer with the given size (if no such size given then the default size is 8Kb).
If we use only the FileReader, then it reads one character at a time by directly accessing the disk drive.
Each time it reads the data, it takes some time for the disk drive to position the read head correctly which makes it very inefficient.
Accessing the disk drive every time for reading a single character affects the performance.
Whereas, BufferedReader creates a buffer, and reads large amount of data using the given FileReader and loads it into a input buffer in the memory.
Each time you want to read the file data, you can read it from the buffer( you don’t have to directly access the disk drive every time) and hence is faster than FileReader.
Time taken : 3
Example 2: Reading lines using only the FileReader
Java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class GFG {
public static void main(String[] args)
{
FileReader reader;
try {
reader = new FileReader( "geeks.txt" );
char ch;
int i = - 1 ;
long initialTime = System.currentTimeMillis();
while ( true ) {
try {
i = reader.read();
}
catch (IOException e) {
e.printStackTrace();
}
if (i == - 1 )
break ;
ch = ( char )i;
System.out.print(ch);
}
System.out.println();
System.out.println( "Time taken : "
+ (System.currentTimeMillis()
- initialTime));
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
|
Output: From the file ‘geeks.txt’ in the local directory used in the above program
Hello geeks!
BufferedReader uses a FileReader to read data from this file.
BufferedReader creates a input buffer with the given size (if no such size given then the default size is 8Kb).
If we use only the FileReader, then it reads one character at a time by directly accessing the disk drive.
Each time it reads the data, it takes some time for the disk drive to position the read head correctly which makes it very inefficient.
Accessing the disk drive every time for reading a single character affects the performance.
Whereas, BufferedReader creates a buffer, and reads large amount of data using the given FileReader and loads it into a input buffer in the memory.
Each time you want to read the file data, you can read it from the buffer( you don’t have to directly access the disk drive every time) and hence is much more efficient than FileReader.
Time taken : 32
Note:
- The time taken for the reading process might vary in your system, but the fact is that BufferedReader works faster and more efficiently than the FileReader.
- If you want a good performance, then you would you use both of them together. BufferedReader itself does not read data from an input stream, it uses a Reader(usually a FileReader) that interacts with the native system APIs to read characters from the given input source( A file in case of a FileReader ). The BufferedReader class just adds a buffer to the character stream and reads characters from the buffer, not directly from the input source. So, you can read a file using only the FileReader because it has access to the hard drive to read data from. But you can not use “only BufferedReader” to read a file, as it doesn’t have the access to the hard drive, you have to provide a Reader( a FileReader) that has the access.
Similar Reads
Difference Between FileInputStream and FileReader in Java
Let us first do discuss them in order to get the understanding alongside an example to interpret the differences. Here first we will be discussing out FileReader class. So starting of with FileReader class in java is used to read data from the file. It returns data in byte format like FileInputStrea
4 min read
Difference between Buffered and Unbuffered Memory
In Computers when we are upgrading or building our device, we might come across two types of memory, which are Buffered and Unbuffered. These terms refer to how a computer's memory helps in making our system stable, fast, and reliable, as well as how it handles data. Buffered memory also known as re
5 min read
Difference between Spooling and Buffering
In computing, efficient management is crucial when processing data or transmitting it. Two ways by which Input/output subsystems can improve the computer's performance and efficiency by using memory space in the main memory or on the disk are spooling and buffering. These are two techniques widely u
5 min read
Difference between Buffer and Cache
Among all the categories existing in the field of computing, both buffers and caches are important for improving system efficiency. But they are different from each other concerning their functions and the modes they employ. Thus, it is vital to comprehend the difference between a buffer and a cache
5 min read
Difference Between Buffering and Caching in OS
Buffering and caching are related to storage and then access of data, but some key difference makes them different. Buffer is a memory space that stores the input data and passes it on to the system where as Caching is storing data in a separate disk. In this article, we will see what both these ter
6 min read
Difference between File and Folder
File: A File is defined as a set of related data or information that is being stored in secondary storage. A file is data file or a program file where former contains data and information in the form of alphanumeric, numeric or binary and latter containing the program code and can also be executed,
2 min read
Difference between Program and File
1. Program : Program, as name suggest, are simple executable files that contain set or collection of instructions used by computer to execute or complete particular tasks as well as produce results you want. 2. File : File, as name suggests, is basic concept in computer that is designed to store dat
2 min read
Difference Between FileInputStream and ObjectInputStream in Java
FileInputStream class extracts input bytes from a file in a file system. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. It should be used to read byte-oriented data for example to read audio, video, images,
5 min read
Difference between Java IO and Java NIO
Java IO(Input/Output) is used to perform read and write operations. The java.io package contains all the classes required for input and output operation. Whereas, Java NIO (New IO) was introduced from JDK 4 to implement high-speed IO operations. It is an alternative to the standard IO API's. In this
3 min read
Difference Between Length and Capacity in Java
Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the
3 min read