Home Java Javafile What is the use of BufferedReader in Java program?
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

What is the use of BufferedReader in Java program?

Advertisement
Learn the importance of BufferedReader class while writing code for reading the input stream. You will get to know the features of BufferedReader in Java program.

Learn about BufferedReader class and understand "What is the use of BufferedReader in Java program?"

In this article I will explain you the benefits and uses of BufferedReader class in Java. In Java there are classes for reading the file/input stream (source stream). With help of InputStreamReader you can read the data into character stream. The InputStreamReader class reads the bytes from stream and then decode into characters. The InputStreamReader class uses the charset to decode the byte into characters.

The BufferedReader class provides advanced functionality of reading the character from input stream and then buffering it for high performance reading of the stream into characters, arrays and lines. If you are writing a program to read a text file then you can use both classes InputStreamReader  and BufferedReader classes for reading the file line by line very efficiently.

How to user the BufferedReader Class?

The BufferedReader class is very easy to use class of the java.io package in Java. You can easily instantiate the class by passing the Reader object as constructor parameter. In this case default buffer size is used, which is sufficient for most of the programming purposes. But you can also specify the buffer size as second argument of the constructor.

Here is the Constructor details and Description of the BufferedReader class:

BufferedReader(Reader in) - This constructor is used to create the object of BufferedReader class by using the default buffer size.

BufferedReader(Reader in, int sz) - This constructor is used when you have to specify the buffer size to be used while reading the data.

Mostly the read() and readLine() methods of the class is used for reading the buffered data.

The method read() - This method is used to reads a single character from the stream.

The method readLine() - This method is used to read a line of text from the stream.

Benefits of BufferedReader class

  • The BufferedReader is used to provide the buffering to the Reader's object while reading the data from input stream.
  • The BufferedReader class increases the efficiency of the program. Your program run fast due to buffering and efficient reading done by the BufferedReader class.
  • The BufferedReader class reads the larger block of data at a time.
  • The BufferedReader class automatically converts the byte data into character data.
  • This class is used to increase the performance of the application

Example of BufferedReader class

Our example code given here will read the file line by line and print on the console. Create a new file "mytext.txt" and add the following content into it:

Tutorials:
Hibernate Framework
Struts Framework
Spring Framework
XML
Ajax
JavaScript
Java
Web Services
Database
Technology
Web Development
PHP

Then create a new Java file with the following code:

import java.io.*;
/**
* @author Deepak Kumar
* Example usase of BufferedReader class in Java
*/
public class BufferedReaderExample
{
    public static void main(String args[]) throws IOException 
    {
        FileReader fileReader=new FileReader("mytext.txt");
        BufferedReader bufferedReader=new BufferedReader(fileReader);
        String str="";
        while((str=bufferedReader.readLine())!=null){
            System.out.println("Data is: " + str);
		}
        fileReader.close();
    }
}

Compile the program and execute it. After execution it will display the data from the text file on console. Here is the output of the program:

D:\upload>cd \Tutorials\Examplecode

D:\Tutorials\Examplecode>javac BufferedReaderExample.java

D:\Tutorials\Examplecode>java BufferedReaderExample
Data is: Tutorials:
Data is: Hibernate Framework
Data is: Struts Framework
Data is: Spring Framework
Data is: XML
Data is: Ajax
Data is: JavaScript
Data is: Java
Data is: Web Services
Data is: Database
Data is: Technology
Data is: Web Development
Data is: PHP
D:\Tutorials\Examplecode>

Read more tutorials at:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 21, 2013

Related Tutorials

Ask Questions?    Discuss: What is the use of BufferedReader in Java program?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com