The Wayback Machine - https://web.archive.org/web/20240921034125/https://www.geeksforgeeks.org/difference-between-scanner-and-bufferreader-class-in-java/
Open In App

Difference Between Scanner and BufferedReader Class in Java

Last Updated : 21 Sep, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Java, Scanner and BufferedReader class are sources that serve as ways of reading inputs. Scanner class is a simple text scanner that can parse primitive types and strings. It internally uses regular expressions to read different types while on the other hand BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of the sequence of characters

The eccentric difference lies in reading different ways of taking input via the next() method that is justified in the below programs over a similar input set.

Example 1:

Java




// Java Program to Illustrate Scanner Class
 
// Importing Scanner class from
// java.util package
import java.util.Scanner;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Creating object of Scanner class to
        // read input from keyboard
        Scanner scn = new Scanner(System.in);
 
        System.out.println("Enter an integer & a String");
 
        // Using nextInt() to parse integer values
        int a = scn.nextInt();
 
        // Using nextLine() to parse string values
        String b = scn.nextLine();
 
        // Display name and age entered above
        System.out.printf("You have entered:- " + a + " "
                          + "and name as " + b);
    }
}


Output:

Enter an integer & a String
10 John
You have entered:- 10 and name as  John

Let us try the same using Buffer class and the same Input below as follows: 

Example 2:

Java




// Java Program to Illustrate BufferedReader Class
 
// Importing required class
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String args[])
        throws IOException
    {
 
        // Creating object of class inside main() method
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));
 
        System.out.println("Enter an integer");
 
        // Taking integer input
        int a = Integer.parseInt(br.readLine());
 
        System.out.println("Enter a String");
 
        String b = br.readLine();
 
        // Printing input entities above
        System.out.printf("You have entered:- " + a
                          + " and name as " + b);
    }
}


Output:

Outputs explanation: In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine() does not read values from console and cursor will not come into console it will skip that step. The nextXXX() methods are nextInt(), nextFloat(), nextByte(), nextShort(), nextDouble(), nextLong(), next().

In BufferReader class there is no such type of problem. This problem occurs only for the Scanner class, due to nextXXX() methods ignoring newline character and nextLine() only reads till the first newline character. If we use one more call of nextLine() method between nextXXX() and nextLine(), then this problem will not occur because nextLine() will consume the newline character. 

Tip: See this for the corrected program. This problem is same as scanf() followed by gets() in C/C++. This problem can also be solved by using next() instead of nextLine() for taking input of strings as shown here.

Following are the Major Differences between Scanner and BufferedReader Class in Java

  • BufferedReader is synchronous while Scanner is not. BufferedReader should be used if we are working with multiple threads.
  • BufferedReader has a significantly larger buffer memory than Scanner.
  • The Scanner has a little buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer), but it’s more than enough.
  • BufferedReader is a bit faster as compared to Scanner because the Scanner does the parsing of input data and BufferedReader simply reads a sequence of characters.



Previous Article
Next Article

Similar Reads

Why BufferedReader class takes less time for I/O operation than Scanner class in java
In Java, both BufferReader and Scanner classes can be used for the reading inputs, but they differ in the performance due to their underlying implementations. The BufferReader class can be generally faster than the Scanner class because of the way it handles the input and parsing. This article will guide these difference, provide the detailed expla
3 min read
Java.io.BufferedReader Class in Java
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made by a Reader causes a corresponding read request to be made of
3 min read
Difference Between BufferedReader and FileReader in Java
BufferedReader and FileReader both classes are used to read data from a given character stream. Both of them have some pros and some cons. In this article, we will discuss the differences between them. Though the most important difference is in the way they work, but we will discuss the other details also. What is a Buffer? A buffer is a small port
9 min read
BufferedReader Class lines() method in Java with Examples
BufferedReader.lines() is the method of the Java Buffered Reader Class in the Java Library which returns lines in terms of Stream and from this Buffered Reader class. With the help of the stream, there are a lot of methods that mimic the output according to our needs. Syntax: BufferedReader.lines() : Stream<String> Parameters: This method doe
2 min read
BufferedReader reset() method in Java with Examples
The reset() method of BufferedReader class in Java is used to fix or mark the position at the last marked position so that the same byte can be read again. Syntax: public void reset() throws IOException Overrides: It overrides the reset() method of Reader class. Parameters: The method does not accept any parameter. Return value: The method does not
3 min read
BufferedReader mark() method in Java with Examples
The mark() method of BufferedReader class in Java is used to mark the current position in the buffer reader stream. The reset() method of the same BufferedReader class is also called subsequently, after the mark() method is called. The reset() method fixes the position at the last marked position so that same byte can be read again. Syntax: public
3 min read
BufferedReader markSupported() method in Java with Examples
The markSupported() method of BufferedReader class in Java is used to verify whether the stream supports the mark() method or not. It returns the boolean value true if the stream supports mark() otherwise it returns false. Syntax: public boolean markSupported() Overrides: It overrides the markSupported() method of the Reader class. Parameters: This
2 min read
BufferedReader close() method in Java with Examples
The close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Syntax: public void close() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return any value. Exception: This method throws IOExceptio
2 min read
BufferedReader ready() method in Java with Examples
The ready() method of BufferedReader class in Java is used to verify whether the buffer stream is ready to be read or not. A buffer stream is said to be ready in two cases either the buffer is not empty or the main stream is ready. Syntax: public boolean ready() throws IOException Overrides: This method overrides ready() method of Reader class. Par
2 min read
BufferedReader read() method in Java with Examples
The read() method of BufferedReader class in Java is of two types: 1. The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value. Syntax: public int read() throws IOException Overrides
3 min read
BufferedReader readLine() method in Java with Examples
The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by '\n' or '\r' or EOF. Syntax: public String readLine() throws IOException Parameters: This method does not accept any parameter. Return value: This method returns the String that is read by this method and excludes
2 min read
BufferedReader skip(long) method in Java with Examples
The skip() method of BufferedReader class in Java is used to skip characters in the stream. The number of characters to be skipped is passed as parameter in this method. Syntax: public long skip(long n) throws IOException Overrides: This method overrides skip() method of Reader class. Parameters: This method accepts one parameter i.e. n which repre
2 min read
Why Does BufferedReader Throw IOException in Java?
IOException is a type of checked exception which occurs during input/output operation. BufferedReader is used to read data from a file, input stream, database, etc. Below is the simplified steps of how a file is read using a BufferedReader in java. In RAM a buffered reader object is created.Some lines of a file are copied from secondary memory ( or
2 min read
Scanner Class in Java
In Java, Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double, etc. and strings. Using the Scanner class in Java is the easiest way to read input in a Java program, though not very efficient if you want an input method for scenarios where time is a constraint like in competitive programming. J
4 min read
Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
Java Deprecated API Scanner tool: Java Deprecated API Scanner tool i.e. jdeprscan is a static analyzing command-line tool which is introduced in JDK 9 for find out the uses of deprecated API in the given input. Here the input can be .class file name, directory or JAR file. Whenever we provide any input to jdeprscan command line tool then it generat
2 min read
Scanner and nextChar() in Java
Scanner class in Java supports nextInt(), nextLong(), nextDouble() etc. But there is no nextChar() (See this for examples) To read a char, we use next().charAt(0). next() function returns the next token/word in the input as a string and charAt(0) function returns the first character in that string, the number 0 in the function in CharAt(NUMBER) rep
1 min read
Scanner nextFloat() method in Java with Examples
The nextFloat() method of java.util.Scanner class scans the next token of the input as a Float(). If the translation is successful, the scanner advances past the input that matched. Syntax: public float nextFloat() Parameters: The function does not accepts any parameter. Return Value: This function returns the Float scanned from the input. Exceptio
4 min read
Scanner skip() method in Java with Examples
skip(Pattern pattern) The skip(Pattern pattern) method of java.util.Scanner class skips input that matches the specified pattern, ignoring the delimiters. The function skips the input if an anchored match of the specified pattern succeeds it. Syntax: public Scanner skip(Pattern pattern) Parameters: The function accepts a mandatory parameter pattern
5 min read
Scanner useRadix() method in Java with Examples
The useRadix(radix) method of java.util.Scanner class sets this scanner's default radix to the specified radix. A scanner's radix affects elements of its default number matching regular expressions. Syntax: public Scanner useRadix(int radix) Parameters: The function accepts a mandatory parameter radix which specifies the radix to use when scanning
2 min read
Scanner close() method in Java with Examples
The close() method ofjava.util.Scanner class closes the scanner which has been opened. If the scanner is already closed then on calling this method, it will have no effect. Syntax: public void close() Return Value: The function does not return any value. Below programs illustrate the above function: Program 1: // Java program to illustrate the // c
1 min read
Scanner delimiter() method in Java with Examples
The delimiter() method ofjava.util.Scanner class returns the Pattern this Scanner is currently using to match delimiters. Syntax: public Pattern delimiter() Return Value: The function returns the scanner's delimiting pattern. Below programs illustrate the above function: Program 1: // Java program to illustrate the // delimiter() method of Scanner
2 min read
Scanner findInLine() method in Java with Examples
findInLine(Pattern pattern) The findInLine(Pattern pattern) method of java.util.Scanner class tries to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is
4 min read
Scanner useLocale() method in Java with Examples
The useLocale() method of java.util.Scanner class sets this scanner's locale to the specified locale. Syntax: public Scanner useLocale(Locale locale) Parameters: The function accepts a mandatory parameter locale which specifies a string specifying the locale to use. Return Value: The function returns this modified scanner. Exceptions: If the radix
2 min read
Scanner toString() method in Java with Examples
The toString() method of java.util.Scanner class returns the string representation of this Scanner. The exact format is unspecified. Syntax: public String toString() Return Value: This function returns the string representation of this scanner. Below program illustrates the above function: Program 1: // Java program to illustrate the // toString()
1 min read
Scanner useDelimiter() method in Java with Examples
useDelimiter(Pattern pattern) The useDelimiter(Pattern pattern) method of java.util.Scanner class sets this scanner's delimiting pattern to the specified pattern. Syntax: public Scanner useDelimiter(Pattern pattern) Parameter: The function accepts a mandatory parameter pattern which specifies a delimiting pattern. Return Value: The function returns
2 min read
Scanner reset() method in Java with Examples
The reset() method of java.util.Scanner class resets this scanner. On resetting a scanner, it discards all of its explicit state information which may have been changed by invocations of useDelimiter(java.util.regex.Pattern), useLocale(java.util.Locale), or useRadix(int). Syntax: public Scanner reset() Return Value: This function returns this scann
2 min read
Scanner radix() method in Java with Examples
The radix() method of java.util.Scanner class returns this scanner's default radix. Syntax: public int radix() Return Value: This function returns the default radix of this scanner. Below programs illustrate the above function: Program 1: // Java program to illustrate the // radix() method of Scanner class in Java import java.util.*; public class G
2 min read
Scanner nextShort() method in Java with Examples
The nextShort(radix) method of java.util.Scanner class scans the next token of the input as a short. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextShort(radix) where the radix is assumed to be the default radix. Syntax: public short nextShor
5 min read
Scanner nextBigInteger() method in Java with Examples
The nextBigInteger(radix) method of java.util.Scanner class scans the next token of the input as a BigInteger. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextBigInteger(radix) where the radix is assumed to be the default radix. Syntax: public
5 min read
Scanner nextByte() method in Java with Examples
The nextByte(radix) method of java.util.Scanner class scans the next token of the input as a Byte. If the translation is successful, the scanner advances past the input that matched. If the parameter radix is not passed, then it behaves similarly as nextByte(radix) where the radix is assumed to be the default radix. Syntax: public byte nextByte() P
5 min read
Article Tags :
Practice Tags :