Skip to main content
Browse topics

Buffered Reader Example in Java

2 min read Updated May 29, 2026
Share

Introduction

Buffered Reader is a classic Java console program that demonstrates the concept with complete source code and sample output. Java I/O reads and writes bytes and characters from files, streams and the console.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

BufferedReader is a wrapper for both “InputStreamReader/FileReader”, which buffers the information each time a native I/O is called. The buffer size may be specified, or the default size may be used. Each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream.

Syntax

java
BufferedReader Variable_name = new BufferedReader("Directory_Of_The_File");

Buffered Reader Example Program

java
import java.io.*;

public class BufferedReaderDemo {
	public static void main(String[] args) throws Exception {
		String  str = null
		try{
			BufferedReader buffread = new BufferedReader("c:/newfile.txt");// Assuming a text file newfile.txt containing data "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			while ((str = buffread.readLine()) != null) {
				System.out.println(str);
			}       
		}catch(Exception e){
		e.printStackTrace();
		}
	}
}

Sample Output

plaintext
ABCDEFGHIJKLMNOPQRSTUVWXYZ

When to use

Use this buffered reader example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.io.*; imports a class used later in the program.

  3. String str = null updates a variable used in the calculation or output.

  4. BufferedReader buffread = new BufferedReader("c:/newfile.txt");// Assuming a text file newfile.txt containing data "ABCDEFGHIJKLMNOPQRSTUVWXYZ" updates a variable used in the calculation or output.

  5. while ((str = buffread.readLine()) != null) { updates a variable used in the calculation or output.

  6. A println / print call writes text to the console — part of the sample output below.

  7. Compare your console output with the sample output for Buffered Reader to confirm the program behaves correctly.

Best Practices

Common Mistakes

Frequently Asked Questions

What does the Buffered Reader program demonstrate?
It shows how to implement buffered reader in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related tutorials

Search tutorials