Java/J2EE and SOA (304 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (303 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

BufferedReader in Java : How To Read Text From Input Stream

Published on Aug 09,2019 472 Views
6 / 12 Blog from Java Strings

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Java provides several mechanisms in order to read from a file. One important class that helps in performing this operation is the BufferedReader. So, this article on BufferedReader in Java will help you in understanding Bufferedreader class along with examples. Following are the topics covered in this blog:

What is BufferedReader in Java?

BufferedReader is a Java class that reads text from the input stream. It buffers the characters so that it can get the efficient reading of characters, arrays, etc. It inherits the reader class and makes the code efficient since we can read the data line-by-line with the readline() method. There are a few pointers we have to keep in mind while working with BufferedReader class in Java.

  • We may have to specify the buffer size even though the default is large enough for any purpose.
  • With each request made of a reader a corresponding, a read request is also made of an underlying character.
  • It is always advised to wrap a BufferedReader class around any reader such as InputStreamReaders.
  • For the programs that use DataInputaStreams for textual input, an appropriate BufferedReader replaces the DataInputStream to localize it.

BufferedReader Class Declaration

public class BufferedReader extends Reader

Java BufferedReader Constructors

ConstructorDescription

BufferedReader(Reader reader)

This constructor creates a buffering character-input stream that works on a default-size input buffer.

BufferedReader(Reader reader, int size)

It uses the specified size for the input buffer for buffering the character-input stream.

Methods And Description

Following are the methods with the description that we have for the Java BufferedReader class.

MethodDescription

int read()

Reads a single character

String readLine()

It reads a line of text

void reset()

Repositions the stream to the position where the mark method was last called

int read(char[]  cb, int off , int len)

Reads the characters in a portion of an array

boolean markSupported()

It tests the input stream support for reset and mark method

boolean ready()

It checks whether the input stream is ready for reading

long skip(long n)

skips the characters

void close()

It closes the input stream

void mark(int readAheadLimit)

Used to mark the current position in the stream

Example:

import java.io.*;
public class Example{
public static void main(String args[] throws Exception)
{
FileReader f = new FileReader("filelocation");
BufferedReader b = new BufferedReader(f);

int i ;
while((i = b.read()) != -1){
System.out.println((char) i);
}
b.close();
f.close();

Difference Between Scanner And BufferedReader

BufferedReaderScanner

Synchronous and should be used with multiple threads

Not synchronous and not used with multiple threads

Buffer memory is larger

Buffer memory is smaller

Faster than Scanner

Slower because it does parsing of the input data

There is no ambiguity related to nextline() method

There are a lot of problems with the nextline() method.

Uses buffering to read characters from the character-input stream

It is a simple text scanner which parses primitive types and strings

BufferedReader in JDK7 Example

import java.io.*;
public class Example{
public static void main(String[] args){
try( BufferedReader b = new BufferedReader(new fileReader("filename")));
{
String s;
while((s = b.readLine()) != null){
System.out.println(s);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

Reading Data From Console By InputStreamReader And BufferedReader in Java

import java.io.*;
public class Example{
public static void main(String args[] throws Exception){
InputStreamReader i = new InputStreamReader(system.in);
BufferedReader b = new BufferedReader(i);
System.out.println("Enter Course");
String course = b.readLine();
System.out.pritln("Edureka" + course);
}
} 
Output:Enter Course
       Java
       Edureka Java

Reading Data From Console Until User Writes Stop

import java.io.*;
public class Example{
public static void main(String args[] throws Exception){
InputStreamReader i = new InputStreamReader(system.in);
BufferedReader b = new BufferedReader(i);
string course = "";
while(!name.equals("stop")){
System.out.println("enter course:");
course = b.readLine();
System.out.println("Course is:" + course);
}
b.close();
i.close();
} 
}
Output: enter course:
        Course is: Java
        enter course:
        Course is: stop

This brings us to the end of this article where we have learned how we can read characters from the character-input stream using the BufferedReader class in Java. Hope you are clear with all that has been shared with you in this tutorial.

If you found this article on “BufferedReader in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “BufferedReader in Java” and our team will be glad to answer.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.