The BufferedReader class provides a buffered character reader class. The BufferedReader class provides readLine() method instead of read() method. BufferedReader has two constructors. In the first form a buffered character stream uses a default buffer size. In the second, the size of the buffer is passes into the constructor as show below.
Constructor | Meaning |
BufferedReader(Reader rd); | Construct a buffering character input stream |
BufferedReader(Reader rd, int size) | Construct a buffering character input stream along with given buffered size |
Consider the following example which reads the file file.txt line by line with readLine() method and prints the content of that file using BufferedReader class.
Example:
/**
From bufferedReader.java
*/
import java.io.BufferedReader;
import java.io.FileReader;
/**
*
* @author JavaHotSpot
*/
public class bufferedReader
{
public static void main(String args[])
{
try
{
FileReader fr = new FileReader("C:\\file.txt");
BufferedReader buff = new BufferedReader(fr);
String ss;
System.out.println("File Content is \n");
while((ss=buff.readLine())!=null)
{
System.out.println(ss);
}
}
catch(Exception ex)
{
System.err.println(ex);
}
}
}
Output:
File Content is
A stream represents a flow of data,
or a channel of communication with a
writer at one end and a reader at the other.
To handle input and output we use java.io package.
Related Articles: