BufferedReader Class

Package: java.io

Reads text from a character input stream, buffering the input to provide efficient reading.

CrossRef.eps The BufferedReaderStream class is one of many Java I/O classes that use streams. For more information, see Streams (Overview).

Constructor

Constructor

Description

BufferedReader (Reader in)

Creates a buffered reader from any object that extends the Reader class. Typically, you pass this constructor a FileReader object.

Methods

Method

Description

void close()

Closes the file and throws IOException.

int read()

Reads a single character from the file and returns it as an integer. The method returns –1 if the end of the file has been reached. It throws IOException.

String readLine()

Reads an entire line and returns it as a string. The method returns null if the end of the file has been reached. It throws IOException.

void skip(long num)

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();

}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset