Package java.io
Class BufferedReader
java.lang.Object
java.io.Reader
java.io.BufferedReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
- Direct Known Subclasses:
LineNumberReader,PemReader
public class BufferedReader extends Reader
Wraps an existing
Reader and buffers the input. Expensive
interaction with the underlying reader is minimized, since most (smaller)
requests can be satisfied by accessing the buffer alone. The drawback is that
some extra space is required to hold the buffer and that copying takes place
when filling that buffer, but this is usually outweighed by the performance
benefits.
A typical application pattern for the class looks like this:
BufferedReader buf = new BufferedReader(new FileReader("file.java"));
- Since:
- 1.1
- See Also:
BufferedWriter
-
Field Summary
-
Constructor Summary
Constructors Constructor Description BufferedReader(Reader in)Constructs a newBufferedReader, providinginwith a buffer of 8192 characters.BufferedReader(Reader in, int size)Constructs a newBufferedReader, providinginwithsizecharacters of buffer. -
Method Summary
Modifier and Type Method Description voidclose()Closes this reader.voidmark(int markLimit)Sets a mark position in this reader.booleanmarkSupported()Indicates whether this reader supports themark()andreset()methods.intread()Reads a single character from this reader and returns it with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int length)Reads up tolengthcharacters from this reader and stores them atoffsetin the character arraybuffer.StringreadLine()Returns the next line of text available from this reader.booleanready()Indicates whether this reader is ready to be read without blocking.voidreset()Resets this reader's position to the lastmark()location.longskip(long charCount)Skips at mostcharCountchars in this stream.
-
Constructor Details
-
BufferedReader
Constructs a newBufferedReader, providinginwith a buffer of 8192 characters.- Parameters:
in- theReaderthe buffer reads from.
-
BufferedReader
Constructs a newBufferedReader, providinginwithsizecharacters of buffer.- Parameters:
in- theInputStreamthe buffer reads from.size- the size of buffer in characters.- Throws:
IllegalArgumentException- ifsize <= 0.
-
-
Method Details
-
close
Closes this reader. This implementation closes the buffered source reader and releases the buffer. Nothing is done if this reader has already been closed.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException- if an error occurs while closing this reader.
-
mark
Sets a mark position in this reader. The parametermarkLimitindicates how many characters can be read before the mark is invalidated. Callingreset()will reposition the reader back to the marked position ifmarkLimithas not been surpassed.- Overrides:
markin classReader- Parameters:
markLimit- the number of characters that can be read before the mark is invalidated.- Throws:
IllegalArgumentException- ifmarkLimit < 0.IOException- if an error occurs while setting a mark in this reader.- See Also:
markSupported(),reset()
-
markSupported
public boolean markSupported()Indicates whether this reader supports themark()andreset()methods. This implementation returnstrue.- Overrides:
markSupportedin classReader- Returns:
trueforBufferedReader.- See Also:
mark(int),reset()
-
read
Reads a single character from this reader and returns it with the two higher-order bytes set to 0. If possible, BufferedReader returns a character from the buffer. If there are no characters available in the buffer, it fills the buffer and then returns a character. It returns -1 if there are no more characters in the source reader.- Overrides:
readin classReader- Returns:
- the character read or -1 if the end of the source reader has been reached.
- Throws:
IOException- if this reader is closed or some other I/O error occurs.
-
read
Reads up tolengthcharacters from this reader and stores them atoffsetin the character arraybuffer. Returns the number of characters actually read or -1 if the end of the source reader has been reached. If all the buffered characters have been used, a mark has not been set and the requested number of characters is larger than this readers buffer size, BufferedReader bypasses the buffer and simply places the results directly intobuffer.- Specified by:
readin classReader- Throws:
IndexOutOfBoundsException- ifoffset < 0 || length < 0 || offset + length > buffer.length.IOException- if this reader is closed or some other I/O error occurs.
-
readLine
Returns the next line of text available from this reader. A line is represented by zero or more characters followed by'\n','\r',"\r\n"or the end of the reader. The string does not include the newline sequence.- Returns:
- the contents of the line or
nullif no characters were read before the end of the reader has been reached. - Throws:
IOException- if this reader is closed or some other I/O error occurs.
-
ready
Indicates whether this reader is ready to be read without blocking.- Overrides:
readyin classReader- Returns:
trueif this reader will not block whenreadis called,falseif unknown or blocking will occur.- Throws:
IOException- if this reader is closed or some other I/O error occurs.- See Also:
read(),read(char[], int, int),readLine()
-
reset
Resets this reader's position to the lastmark()location. Invocations ofread()andskip()will occur from this new location.- Overrides:
resetin classReader- Throws:
IOException- if this reader is closed or no mark has been set.- See Also:
mark(int),markSupported()
-
skip
Skips at mostcharCountchars in this stream. Subsequent calls toreadwill not return these chars unlessresetis used.Skipping characters may invalidate a mark if
markLimitis surpassed.- Overrides:
skipin classReader- Returns:
- the number of characters actually skipped.
- Throws:
IllegalArgumentException- ifcharCount < 0.IOException- if this reader is closed or some other I/O error occurs.- See Also:
Reader.mark(int),Reader.markSupported(),Reader.reset()
-