Home > DeveloperSection > Forums > Difference between buffered reader and file reader
Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    March-16-2015 7:58 AM

 Java Java 
Ratings:


 2 Reply(s)
 772  View(s)
Rate this:
can anyone explain me the difference between the class BufferedReader  and FileReader ? and which one to use when i want to read a text file Thank you


Pawan Shukla
Pawan Shukla

Total Post:29

Points:203
Posted on    March-16-2015 8:01 AM

  • 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. Personally I find the API of Scanner to be pretty painful and obscure.

To read a text file, I would suggest using a FileInputStream wrapped in an InputStreamReader (so you can specify the encoding) and then wrapped in a BufferedReader for buffering and the ability to read a line at a time.

Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    March-16-2015 8:03 AM

And as per question for reading a text file you should use BufferedReader because Scanner hides IOException while BufferedReader throws it immediately.

BufferedReader is synchronized and Scanner is not.

Scanner:- is used for parsing tokens from the contents of the stream.

BufferedReader:- BufferedReader just reads the stream.

Don't want to miss updates? Please click the below button!

Follow MindStick