• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java BufferedReader Example

April 17, 2014 //  by Krishna Srinivasan//  Leave a Comment

In our previous example I have explained about the FileReader. FileReader is useful for reading the text content by characters. This is slow in performance when using for the huge files. BufferedReader is buffering the text for Reader’s. Buffering will speed up the IO operation. Instead of reading one character at a time using the FileReader, BufferedReader will be fast for reading the huge amount of data. Also it helps you to read the text file line by line.

The main difference between BufferedReader and BufferedInputStream is that Reader’s work on characters (text), wheres InputStream’s works on raw bytes. Each API can be chosen for the suitable scenarios.

Lets look at the below example. We need to wrap the FileReader into the BufferedReader to read a text file.
BufferedReaderExample.java

package javabeat.net.core;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * File BufferedReader Example
 *
 * @author Krishna
 *
 */
public class BufferedReaderExample {
	public static void main(String args[]) {
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		String str = null;
		try {
			fileReader = new FileReader("TextFile.txt");
			bufferedReader = new BufferedReader(fileReader);
			while ((str = bufferedReader.readLine()) != null) {
				System.out.println(str);
			}
			bufferedReader.close();
		} catch (FileNotFoundException exception) {
			exception.printStackTrace();
		} catch (IOException exception) {
			exception.printStackTrace();
		}
	}
}

TextFile.txt

Reading
the
text
content
using BufferedReader!!

Output…

Reading
the
text
content
using BufferedReader!!

Category: JavaTag: Java File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java FileReader Example
Next Post: Java LineNumberReader Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact