Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between buffered reader and file reader and scanner class [duplicate]

Can anyone explain me the difference between the class BufferedReader, FileReader and Scanner? and which one to use when I want to read a text file?

like image 266
Charbel Avatar asked Dec 30 '13 11:12

Charbel


People also ask

What is the difference between buffer reader and Scanner?

Scanner and BufferReader both classes are used to read input from external system. Scanner is normally used when we know input is of type string or of primitive types and BufferReader is used to read text from character streams while buffering the characters for efficient reading of characters.

Is Buffered reader better than Scanner?

BufferedReader is a bit faster as compared to scanner because scanner does parsing of input data and BufferedReader simply reads sequence of characters.

What is the difference between BufferedReader and BufferedInputStream?

The main difference between BufferedReader and BufferedInputStream is that BufferedReader reads characters (text), whereas the BufferedInputStream reads raw bytes. The Java BufferedReader class is a subclass of the Java Reader class, so you can use a BufferedReader anywhere a Reader is required.

What is the difference between InputStreamReader and BufferedReader?

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.


2 Answers

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

Alternatively, you could use a third-party library which makes it simpler, such as Guava:

File file = new File("foo.txt");
List<String> lines = Files.readLines(file, Charsets.UTF_8);

Or if you're using Java 7, it's already available for you in java.nio.file.Files:

Path path = FileSystems.getDefault().getPath("foo.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
like image 79
Jon Skeet Avatar answered Oct 04 '22 21:10

Jon Skeet


And as per your 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 just reads the stream.

For more info follow the link (http://en.allexperts.com/q/Java-1046/2009/2/Difference-Scanner-Method-Buffered.htm)

like image 38
Saurabh Sharma Avatar answered Oct 04 '22 21:10

Saurabh Sharma