Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Design the UI

Grigorij Schleifer
Grigorij Schleifer
10,365 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.