Advertisement



< Prev
Next >



BufferedReader



File Input/Output operations consume a lot of important resources and are time consuming. Hence, reading a chunk of characters out of a file and storing it in a local buffer for later processing is faster and than reading a character at a time, out of a file. Such buffering speeds up the I/O process.

BufferedReader class is used to create such buffered reader stream through which a chunk of characters are read out of a file and transferred to a local buffer for later use.




Constructors :


BufferedReader(Reader r)
This constructor creates a BufferedReader object to read characters out of a file, which is accessed by a Reader, passed to this constructor as a parameter.

Example-:
File file= new File("D:/Textbook.txt");
FileReader fr= new FileReader(file);
BufferedReader br= new BufferedReader(fr);
This example has created a BufferedReader object to read characters out of a file D:/Textbook.txt using Reader i.e. FileReader. As soon as this constructor is called, a large chunk of characters are read out of file and stored in the local buffer of BufferedReader.




Point to remember


BufferedReader is a subclass of Reader class.




Methods for reading BufferedReader


Some methods that we generally use while working with BufferedReader are shown in the table below :

Methods Description
available()
This method gives the remaining number of bytes available to read from this input character stream's buffer.

read()
This method reads one character at a time from this input character stream's buffer.

read(char[])
This method reads a whole character array at a time from this input character stream's buffer.

close()
This method closes this input stream and also frees any resources connected with this input stream.




Advertisement




Program to read one character a time from a file using BufferedReader.


We are creating a BufferedReader object to read characters from a file D:\\TextBook.txt, referenced by FileReader object. Let's the contents of this file are -:
Hello from Java
Next topic is BufferedWriter class


//A program to create a BufferedReader Stream for reading characters from a local buffer. 

import java.io.*;
class A
{
public static void main(String... ar)
{

try
{
File file= new File("TextBook9.txt");
FileReader fr= new FileReader(file); 
BufferedReader br = new BufferedReader(fr); //This will transfer chunk of characters 
					    //from file to buffer of BufferedReader
								
int c;

//Reading one character a time from buffer
while( (c=br.read())!=-1)      
{
System.out.print((char)c);
}

br.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}


Output is -:


Hello from Java!
Next topic is BufferedWriter class


Program Analysis






Please share this article -




< Prev
Next >
< FileWriter
BufferedWriter >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement