Skip to content

Most visited

Recently visited

navigation
Added in API level 1

BufferedReader

public class BufferedReader
extends Reader

java.lang.Object
   ↳ java.io.Reader
     ↳ java.io.BufferedReader
Known Direct Subclasses


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

See also:

Summary

Inherited fields

From class java.io.Reader

Public constructors

BufferedReader(Reader in)

Constructs a new BufferedReader, providing in with a buffer of 8192 characters.

BufferedReader(Reader in, int size)

Constructs a new BufferedReader, providing in with size characters of buffer.

Public methods

void close()

Closes this reader.

void mark(int markLimit)

Sets a mark position in this reader.

boolean markSupported()

Indicates whether this reader supports the mark() and reset() methods.

int read()

Reads a single character from this reader and returns it with the two higher-order bytes set to 0.

int read(char[] buffer, int offset, int length)

Reads up to length characters from this reader and stores them at offset in the character array buffer.

String readLine()

Returns the next line of text available from this reader.

boolean ready()

Indicates whether this reader is ready to be read without blocking.

void reset()

Resets this reader's position to the last mark() location.

long skip(long charCount)

Skips at most charCount chars in this stream.

Inherited methods

From class java.io.Reader
From class java.lang.Object
From interface java.lang.Readable
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Public constructors

BufferedReader

Added in API level 1
BufferedReader (Reader in)

Constructs a new BufferedReader, providing in with a buffer of 8192 characters.

Parameters
in Reader: the Reader the buffer reads from.

BufferedReader

Added in API level 1
BufferedReader (Reader in, 
                int size)

Constructs a new BufferedReader, providing in with size characters of buffer.

Parameters
in Reader: the InputStream the buffer reads from.
size int: the size of buffer in characters.
Throws
IllegalArgumentException if size <= 0.

Public methods

close

Added in API level 1
void 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.

Throws
IOException if an error occurs while closing this reader.

mark

Added in API level 1
void mark (int markLimit)

Sets a mark position in this reader. The parameter markLimit indicates how many characters can be read before the mark is invalidated. Calling reset() will reposition the reader back to the marked position if markLimit has not been surpassed.

Parameters
markLimit int: the number of characters that can be read before the mark is invalidated.
Throws
IllegalArgumentException if markLimit < 0.
IOException if an error occurs while setting a mark in this reader.

See also:

markSupported

Added in API level 1
boolean markSupported ()

Indicates whether this reader supports the mark() and reset() methods. This implementation returns true.

Returns
boolean true for BufferedReader.

See also:

read

Added in API level 1
int 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.

Returns
int 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

Added in API level 1
int read (char[] buffer, 
                int offset, 
                int length)

Reads up to length characters from this reader and stores them at offset in the character array buffer. 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 into buffer.

Parameters
buffer char
offset int
length int
Returns
int
Throws
IndexOutOfBoundsException if offset < 0 || length < 0 || offset + length > buffer.length.
IOException if this reader is closed or some other I/O error occurs.

readLine

Added in API level 1
String 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
String the contents of the line or null if 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

Added in API level 1
boolean ready ()

Indicates whether this reader is ready to be read without blocking.

Returns
boolean true if this reader will not block when read is called, false if unknown or blocking will occur.
Throws
IOException if this reader is closed or some other I/O error occurs.

See also:

reset

Added in API level 1
void reset ()

Resets this reader's position to the last mark() location. Invocations of read() and skip() will occur from this new location.

Throws
IOException if this reader is closed or no mark has been set.

See also:

skip

Added in API level 1
long skip (long charCount)

Skips at most charCount chars in this stream. Subsequent calls to read will not return these chars unless reset is used.

Skipping characters may invalidate a mark if markLimit is surpassed.

Parameters
charCount long
Returns
long the number of characters actually skipped.
Throws
IllegalArgumentException if charCount < 0.
IOException if this reader is closed or some other I/O error occurs.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.