Back to Study Material Java

BufferedReader and Scanner

Java provides two main classes for reading text input: BufferedReader from java.io and Scanner from java.util. Both can read from files, the console, strin...

BufferedReader and Scanner

Two Ways to Read Text Input in Java

Java provides two main classes for reading text input: BufferedReader from java.io and Scanner from java.util. Both can read from files, the console, strings, or any other input source, but they differ in API design, performance, and capability. Choosing the right one depends on whether you need fast line-by-line reading or flexible token-based parsing.

BufferedReader: Fast and Simple BufferedReader wraps any Reader and adds an internal buffer so the underlying source is read in large chunks rather than one character at a time, dramatically improving performance for large files. Its API is minimal: readLine() returns the next line as a String (or null at end of file), and read() returns a single character as an int. For reading large files quickly—log files, data exports, generated reports—BufferedReader is typically the right choice. Combine it with try-with-resources for clean, safe usage.

Scanner: Flexible Token Parsing Scanner can read tokens from any source: System.in for console input, a File, a String, or any InputStream. It splits input into tokens using a delimiter (whitespace by default) and provides typed methods: nextInt(), nextDouble(), nextBoolean(), and nextLine(). This makes Scanner ideal for parsing structured console input in competitive programming or simple interactive programs. It can also use a regular expression as the delimiter, giving it parsing power that BufferedReader lacks.

Common Pitfall with nextInt and nextLine A classic Scanner bug occurs when you call nextInt() followed by nextLine(). nextInt() reads the integer but leaves the newline character in the buffer. The subsequent nextLine() reads that leftover newline instead of the next line of real input, returning an empty string. The fix is to call nextLine() once after nextInt() to consume the leftover newline before reading the next meaningful line.

Code Example: import java.util.Scanner; import java.io.*;

// BufferedReader for file try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line.trim()); } }

// Scanner for console input Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); sc.nextLine(); // consume leftover newline System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println(name + " is " + age + " years old.");

Interview Tip: A frequent interview follow-up after you mention Scanner is "which is faster for reading large files, Scanner or BufferedReader?" BufferedReader is significantly faster for large text files because Scanner tokenizes each chunk using regex matching, which adds overhead. BufferedReader simply reads bytes and converts them, which is much cheaper. For files in the megabyte range, the difference is measurable and matters in production systems.