BufferedReader Class
Package: java.io
Reads text from a character input stream, buffering the input to provide efficient reading.
Constructor
Constructor |
Description |
|
Creates a buffered reader from any object that extends the |
Methods
Method |
Description |
|
Closes the file and throws |
|
Reads a single character from the file and returns it as an integer. The method returns |
|
Reads an entire line and returns it as a string. The method returns |
|
Skips ahead the specified number of characters. |
Creating a BufferedReader
A BufferedReader
is usually created from a FileReader
, which is in turn created from a File
, like this:
File f = new File(“myfile.txt”);
BufferedReader in;
in = new BufferedReader(new FileReader(f));
Reading from a BufferedReader
To read a line from a BufferedReader
, you use the readLine
method. This method returns null
when the end of the file is reached. As a result, testing the string returned by the readLine
method in a while
loop to process all the lines in the file is common:
String line = in.readLine();
while (line != null)
{
System.out.println(line);
line = in.readLine();
}