[go: up one dir, main page]

0% found this document useful (0 votes)
27 views8 pages

Emo 4

Uploaded by

raroy67751
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views8 pages

Emo 4

Uploaded by

raroy67751
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Skip to content

geeksforgeeks
Courses 90% Refund
Tutorials
Java
Practice
Contests

Sign In

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

Content Improvement Event


Share Your Experiences
Java Tutorial
Overview of Java
Basics of Java
Input/Output in Java
How to Take Input From User in Java?
Scanner Class in Java
Java.io.BufferedReader Class in Java
Difference Between Scanner and BufferedReader Class in Java
Ways to read input from console in Java
System.out.println in Java
Difference between print() and println() in Java
Formatted Output in Java using printf()
Fast I/O in Java in Competitive Programming
Flow Control in Java
Operators in Java
Strings in Java
Arrays in Java
OOPS in Java
Inheritance in Java
Abstraction in Java
Encapsulation in Java
Polymorphism in Java
Constructors in Java
Methods in Java
Interfaces in Java
Wrapper Classes in Java
Keywords in Java
Access Modifiers in Java
Memory Allocation in Java
Classes of Java
Packages in Java
Java Collection Tutorial
Exception Handling in Java
Multithreading in Java
Synchronization in Java
File Handling in Java
Java Regex
Java IO
Java Networking
JDBC - Java Database Connectivity
Java 8 Features - Complete Tutorial
Java Backend DevelopmentCourse
Difference Between Scanner and BufferedReader Class in Java
Last Updated : 21 Sep, 2022
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 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 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.

Three 90 Challenge is back on popular demand! After processing refunds worth INR
1CR+, we are back with the offer if you missed it the first time. Get 90% course
fee refund in 90 days. Avail now!
Want to be a master in Backend Development with Java for building robust and
scalable applications? Enroll in Java Backend and Development Live Course by
GeeksforGeeks to get your hands dirty with Backend Programming. Master the key Java
concepts, server-side programming, database integration, and more through hands-on
experiences and live projects. Are you new to Backend development or want to be a
Java Pro? This course equips you with all you need for building high-performance,
heavy-loaded backend systems in Java. Ready to take your Java Backend skills to the
next level? Enroll now and take your development career to sky highs.

Pranav Adarsh

253
Previous Article
Java.io.BufferedReader Class in Java
Next Article
Ways to read input from console in Java
Read More
Down Arrow
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
Article Tags :
Java
Java-I/O
Practice Tags :
Java
three90RightbarBannerImg

course-img
216k+ interested Geeks
Java Programming Online Course [Complete Beginner to Advanced]
Avail 90% Refund
course-img
30k+ interested Geeks
Manual to Automation Testing: A QA Engineer's Guide
Avail 90% Refund
course-img
217k+ interested Geeks
JAVA Backend Development - Live
Avail 90% Refund

geeksforgeeks-footer-logo
Corporate & Communications Address:- A-143, 9th 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
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
GFG Corporate Solution
Placement Training Program
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
GfG Weekly Contest
Offline Classes (Delhi/NCR)
DSA in JAVA/C++
Master System Design
Master CP
GeeksforGeeks Videos
Geeks Community
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA
Data Structures
Algorithms
DSA for Beginners
Basic DSA Problems
DSA Roadmap
DSA Interview Questions
Competitive Programming
Data Science & ML
Data Science With Python
Data Science For Beginner
Machine Learning Tutorial
ML Maths
Data Visualisation Tutorial
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
Deep Learning Tutorial
Web Technologies
HTML
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS
Python Tutorial
Python Programming Examples
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
OpenCV Tutorial
Python Interview Question
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths
DevOps
Git
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
School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
HR Management
Finance
Income Tax
Databases
SQL
MYSQL
PostgreSQL
PL/SQL
MongoDB
Preparation Corner
Company-Wise Recruitment Process
Resume Templates
Aptitude Preparation
Puzzles
Company-Wise Preparation
Companies
Colleges
Competitive Exams
JEE Advanced
UGC NET
UPSC
SSC CGL
SBI PO
SBI Clerk
IBPS PO
IBPS Clerk
More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
All Cheat Sheets
Recent Articles
Free Online Tools
Typing Test
Image Editor
Code Formatters
Code Converters
Currency Converter
Random Number Generator
Random Password Generator
Write & Earn
Write an Article
Improve an Article
Pick Topics to Write
Share your Experiences
Internships
@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
Got It !
Lightbox

You might also like