Buffered Reader Example Java Program


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

BufferedReader Variable_name = new BufferedReader("Directory_Of_The_File");

Buffered Reader Example Program

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

Output is:
ABCDEFGHIJKLMNOPQRSTUVWXYZ