Java Java Data Structures Efficiency! Design the UI

Grigorij Schleifer
Grigorij Schleifer
10,350 Points

Difference between Scanner and BufferedReader

Hey folks,

can someone explain the difference ...

1 Answer

Enrique Munguía
Enrique Munguía
14,311 Points

Scanner is a class specialized in parsing primitives and strings from a text source, this source can be a text file, the input from the keyboard and so on, it provides methods to detect and find those types among other things. When you start programming in Java you are advise to use Scanner to read user input from the keyboard because of its simplicity.

BufferedReader is a general purpose class for read text from a character-input stream this can be a text file or input from the keyboard too, but as the name implies, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. If the text you are reading contains numbers or other primitives it is up to you to manually parse those primitives to use them in your program.

When to use one or another, my rule of thumb is that if you want to parse primitives from short amount of text you use Scanner, when you want to read large amounts of text (and optionally parse some sections of it) you use BufferedReader.