DIFFERENCE BETWEEN BUFFERED READER AND FILE READER

Mayank Tripathi

Total Post:397

Points:3117
Posted by  Mayank Tripathi
Java 
 1488  View(s)
Ratings:
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
  1. Pawan Shukla

    Post:29

    Points:203
    Re: Difference between buffered reader and file reader

    • 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.

  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Difference between buffered reader and file reader

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!