Scanner vs. BufferedReader
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, both Scanner and BufferedReader are used for reading input data, typically from files or the console. However, they serve different purposes and have different strengths, limitations, and usages. Understanding the distinctions between these two can help in selecting the appropriate tool for specific programming needs.
Scanner
Scanner is a class in Java that simplifies the parsing of primitive types and strings using regular expressions. It can parse and convert the input into different data types, such as int, long, double, etc., directly. This class is commonly used for reading console input as well as for breaking down formatted input data (like CSV files) into tokens.
Features of Scanner:
- Tokenization: Automatically parses primitive types and strings using delimiters, which by default match whitespace.
- Regular Expression Support: Can use custom delimiters defined by regular expressions.
- Ease of Use: Provides methods like
nextInt(),nextDouble(),nextLine(), etc., for various data types. - Locale-Sensitive: Adapts to locale-specific decimal points and other conventions, such as commas for thousands separators.
Example Usage of Scanner:
BufferedReader
BufferedReader, on the other hand, is meant for reading text from input streams, buffering characters for efficient reading of characters, arrays, and lines. It is part of the Java I/O package and provides functionality that is lower-level compared to Scanner.
Features of BufferedReader:
- Efficiency: Reads text efficiently by buffering characters. This buffering capability is useful for reading larger files.
- Reads Lines: Primarily reads data line by line with the
readLine()method. - Flexibility: Since it reads in plain text, it doesn't parse the input. Developers need to manually convert the text into the desired data format.
- Performance: Typically offers better performance in reading large files due to reduced I/O operations.
Example Usage of BufferedReader:
Comparison Table
| Feature | Scanner | BufferedReader |
| Input parsing | Built-in | Manual |
| Delimiters | Customizable | N/A |
| Read by | Tokens | Line |
| Convenience for Primitives | High | Low |
| Performance on Large Files | Lower | Higher |
| API Complexity | Moderate | Simple |
| Use Case | Small files, console input | Large files, efficient character processing |
Subtopics
Handling Binary Data:
Neither Scanner nor BufferedReader is suitable for binary data handling. Instead, classes like InputStream or FileInputStream should be used for binary files.
Thread Safety:
Both Scanner and BufferedReader are not thread-safe. Synchronization must be handled externally if used in a multi-threaded environment.
Buffer Size Customization:
BufferedReader allows setting custom buffer sizes through its constructor, which can be tuned based on performance requirements.
Conclusion
Scanner and BufferedReader offer distinct features that make them suitable for different use cases in Java programming. While Scanner provides convenient data parsing capabilities making it ideal for smaller input and formatted data, BufferedReader excels in handling large files efficiently. Choosing between the two greatly depends on the specific requirements of the application regarding input size, type, and desired processing overhead.
Related reading
- Scanner vs. StringTokenizer vs. String.Split
- Scanning Java annotations at runtime
- Scheduled websocket push with Springboot
- ScheduledExecutorService Exception handling
- Scheduler not running in Spring Boot
- Securing Spring Boot API with API key and secret
- SecurityFilterChain fails to instantiate after spring boot migration from 3.0.8 to 3.0.9
- See all the Java versions installed on Mac

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.