0% found this document useful (0 votes)
248 views

Difference Between Scanner and BufferReader Class in Java

The document discusses the differences between the Scanner and BufferedReader classes in Java. It notes that Scanner has issues when using nextLine() after nextXXX() methods, where it will skip the next input. BufferedReader does not have this issue. It also lists some other differences, such as BufferedReader being synchronous while Scanner is not, and BufferedReader having a larger buffer size. The document then discusses the InputStream and InputStreamReader classes, which allow reading input bytes from a stream. It provides an example of reading from a file input stream using these classes and a BufferedReader. Finally, it briefly discusses the OutputStream class and provides a simple example of writing bytes to an output stream and file.

Uploaded by

Pratik Soni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
248 views

Difference Between Scanner and BufferReader Class in Java

The document discusses the differences between the Scanner and BufferedReader classes in Java. It notes that Scanner has issues when using nextLine() after nextXXX() methods, where it will skip the next input. BufferedReader does not have this issue. It also lists some other differences, such as BufferedReader being synchronous while Scanner is not, and BufferedReader having a larger buffer size. The document then discusses the InputStream and InputStreamReader classes, which allow reading input bytes from a stream. It provides an example of reading from a file input stream using these classes and a BufferedReader. Finally, it briefly discusses the OutputStream class and provides a simple example of writing bytes to an output stream and file.

Uploaded by

Pratik Soni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Difference between Scanner and BufferReader Class

in Java
java.util.Scanner class is a simple text scanner which can parse primitive types and strings. It internally uses
regular expressions to read different types. Java.io.BufferedReader class reads text from a character-input
stream, buffering characters so as to provide for the efficient reading of sequence of characters Following are
differences between above two.

Issue with Scanner when nextLine() is used after nextXXX()


Try to guess the output of following code :

filter_none

edit

play_arrow

brightness_4

// Code using Scanner Class


import java.util.Scanner;
class Differ
{
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
System.out.println("Enter an integer");
int a = scn.nextInt();
System.out.println("Enter a String");
String b = scn.nextLine();
System.out.printf("You have entered:- "
+ a + " " + "and name as " + b);
}
}

Input:

50
Geek

Output:

Enter an integer
Enter a String
You have entered:- 50 and name as

Let us try the same using Buffer class and same Input

filter_none

edit
play_arrow

brightness_4

// Code using Buffer Class


import java.io.*;
class Differ
{
public static void main(String args[])
throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter an integer");
int a = Integer.parseInt(br.readLine());
System.out.println("Enter a String");
String b = br.readLine();
System.out.printf("You have entered:- " + a +
" and name as " + b);
}
}

Input:

50
Geek

Output:

Enter an integer
Enter a String
you have entered:- 50 and name as Geek

In Scanner class if we call nextLine() method after any one of the seven nextXXX() method then the nextLine()
doesn’t 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 Scanner class, due to
nextXXX() methods ignore newline character and nextLine() only reads till 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. See this for the corrected program. This problem is
same as scanf() followed by gets() in C/C++.

Other differences:

 BufferedReader is synchronous while Scanner is not. BufferedReader should be used if we are working
with multiple threads.
 BufferedReader has 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 scanner does parsing of input data and
BufferedReader simply reads sequence of characters.
Java.io.InputStream & InputStreamReader Class
in Java
InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents
input stream of bytes. Applications that are defining subclass of InputStream must provide method, returning
the next byte of input.
A reset() method is invoked which re-positions the stream to the recently marked position.

package com.zetcode;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class JavaInputStreamText {

public static void main(String[] args) throws IOException {

String fileName = "src/resources/thermopylae.txt";

try (InputStream fis = new FileInputStream(fileName);


InputStreamReader isr = new InputStreamReader(fis,
StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {

br.lines().forEach(line -> System.out.println(line));


}
}
}
OUTPUT STREAM READER
This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts
output bytes and sends them to some sink.
Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte
of output.
import java.io.*;
//Java program to demonstrate OutputStream
class OutputStreamDemo
{
public static void main(String args[])throws Exception
{
OutputStream os = new FileOutputStream("file.txt");
byte b[] = {65, 66, 67, 68, 69, 70};

//illustrating write(byte[] b) method


os.write(b);

//illustrating flush() method


os.flush();

//illustrating write(int b) method


for (int i = 71; i <75 ; i++)
{
os.write(i);
}

os.flush();

//close the stream


os.close();
}
}

You might also like