Java
Programming
Input Methods
Scanner
BufferedReader

Scanner vs. BufferedReader

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:
java
1Scanner scanner = new Scanner(System.in);
2System.out.print("Enter your age: ");
3int age = scanner.nextInt();
4scanner.close();

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:
java
1BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
2System.out.print("Enter your name: ");
3String name = reader.readLine();
4reader.close();

Comparison Table

FeatureScannerBufferedReader
Input parsingBuilt-inManual
DelimitersCustomizableN/A
Read byTokensLine
Convenience for PrimitivesHighLow
Performance on Large FilesLowerHigher
API ComplexityModerateSimple
Use CaseSmall files, console inputLarge 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.

java
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 8192); // Buffer size of 8192 characters

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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.