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 ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.