-2

which one should be preferred while dealing with any text file in java and why? Buffered reader or scanner. Please tell advantage and disadvantage of using them in exceptional cases,if any.

I searched this issue in previous questions but asking again to get direct & clear answer.

Stones
  • 13
  • 7
  • `Scanner` is ***very** slow. This shouldn't matter for small files or reading user input (users a far slower than a `Scanner` could ever be). – Boris the Spider Nov 07 '15 at 08:54

2 Answers2

0

A simple look at the documentation and available methods for each will tell you :

  • BufferedReader is much more primitive as you can only read a single char (read) or a full line (readLine)

  • Scanner provides you many more useful method to read many type of data, it also lets you define the way you want to parse your data with regular expression.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
0

I hope this may answer your question what are the benefits of BufferedReader over Scanner

or also

Scanner allows you to parse the input in certain ways (ex. next(), nextInt(), nextDouble(), etc.), while BufferedReader is designed to handle streams without altering the content.

Community
  • 1
  • 1
David Sam
  • 59
  • 9