Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java BufferedReader vs Scanner Class
Next article icon

Java BufferedReader vs Scanner Class

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Java provides several classes for reading input, but two of the most commonly used are Scanner and BufferedReader. The main difference between Scanner and BufferedReader is:

  • Scanner class provides parsing and input reading capabilities with built-in methods for different data types.
  • BufferedReader class reads text efficiently from a character input stream.

Difference Between BufferedReader and Scanner Class

Aspect

Scanner

BufferedReader

Package

It is a part of java.util package.

It is a part of java.io package.

Key use

Simple parsing of primitive types and strings

High-performance text reading

Performance

Performance is slower due to parsing overhead and tokenization

Performance is faster due to efficient buffering

Buffer Size

Buffer Size is smaller

Buffer Size is larger

Thread-safe

It is not thread-safe.

It is thread-safe.

Error Handling

Throws an exception like InputMismatchException

Throws an Exception like IOException

Note: Both Scanner and BufferedReader can read from files.

Example:

new Scanner(new File("input.txt")) or,

new BufferedReader(new FileReader("input.txt"))

Scanner Class

We use the Scanner class when we need to read and parse input directly into primitive data types or strings, especially in small console-based applications or when performance is not a major concern.

Note: Scanner uses whitespace as the default delimiter. If users input multiple values on the same line (e.g., "Sweta 25"), Scanner will read them sequentially. You can change the delimiter using scanner.useDelimiter() if needed.

Example: The below Java program demonstrates the basic input and output operations using the Scanner class.

Java
import java.util.Scanner;  public class Geeks {      public static void main(String[] args)     {         Scanner s = new Scanner(System.in);         System.out.print("Enter your name: ");                String name = s.nextLine();         System.out.print("Enter your age: ");                int age = s.nextInt();         System.out.println("Name: " + name                            + ", Age: " + age);         s.close();     } } 

Output:

Output


BufferedReader Class

We use BufferedReader when performance is important, especially for efficiently reading large volumes of data or files. BufferedReader reads large chunks of data at once, making it ideal for reading from files or processing large amounts of input.

Note: BufferedReader.readLine() throws a checked IOException, so it must be handled using a try-catch block or declared using throws IOException.

Example: The below Java program demonstrates reading user input from the console using BufferedReader.

Java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;  public class Geeks {     public static void main(String[] args)         throws IOException     {         BufferedReader r = new BufferedReader(             new InputStreamReader(System.in));          System.out.print("Enter your name: ");         String name = r.readLine();          System.out.print("Enter your age: ");         int age = Integer.parseInt(r.readLine());          System.out.println("Name: " + name                            + ", Age: " + age);     } } 

Output:

Output

Next Article
Java BufferedReader vs Scanner Class

K

kartik
Improve
Article Tags :
  • Java
  • Java-I/O
Practice Tags :
  • Java

Similar Reads

    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()
    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 a
    2 min read
    Scanner Class in Java
    In Java, the Scanner class is present in the java.util package is used to obtain input for primitive types like int, double, etc., and strings. We can use this class to read input from a user or a file. In this article, we cover how to take different input values from the user using the Scanner clas
    6 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
    3 min read
    Java User Input - Scanner Class
    The most common way to take user input in Java is using the Scanner class. It is a part of java.util package. The scanner class can handle input from different places, like as we are typing at the console, reading from a file, or working with data streams. This class was introduced in Java 5. Before
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences