Class: java.io.BufferedReader
- public class BufferedReader
- extends Reader
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in
= new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each
invocation of read() or readLine() could cause bytes to be read from the
file, converted into characters, and then returned, which can be very
inefficient.
Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.
Methods
-
BufferedReadertop
public BufferedReader(Reader in)Create a buffering character-input stream that uses a default-sized input buffer. -
BufferedReadertop
public BufferedReader(Reader in, int sz)Create a buffering character-input stream that uses an input buffer of the specified size. -
closetop
public void close() throws IOExceptionClose the stream. -
marktop
public void mark(int readAheadLimit) throws IOExceptionMark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. -
markSupportedtop
public boolean markSupported()Tell whether this stream supports the mark() operation, which it does.- Override hierarchy:
- markSupported from Reader
-
readtop
public int read() throws IOExceptionRead a single character. -
readtop
public int read(char[] cbuf, int off, int len) throws IOExceptionRead characters into a portion of an array.This method implements the general contract of the corresponding
readmethod of thejava.io.Readerclass. As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking thereadmethod of the underlying stream. This iteratedreadcontinues until one of the following conditions becomes true:- The specified number of characters have been read,
- The
readmethod of the underlying stream returns-1, indicating end-of-file, or - The
readymethod of the underlying stream returnsfalse, indicating that further input requests would block.
readon the underlying stream returns-1to indicate end-of-file then this method returns-1. Otherwise this method returns the number of characters actually read.Subclasses of this class are encouraged, but not required, to attempt to read as many characters as possible in the same fashion.
Ordinarily this method takes characters from this stream's character buffer, filling it from the underlying stream as necessary. If, however, the buffer is empty, the mark is not valid, and the requested length is at least as large as the buffer, then this method will read characters directly from the underlying stream into the given array. Thus redundant
BufferedReaders will not copy data unnecessarily. -
readLinetop
public String readLine() throws IOExceptionRead a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. -
readytop
public boolean ready() throws IOExceptionTell whether this stream is ready to be read. A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready. -
resettop
public void reset() throws IOExceptionReset the stream to the most recent mark. -
skiptop
public long skip(long n) throws IOExceptionSkip characters.
