BufferedReader

open class BufferedReader @JvmOverloads constructor(in: Reader, sz: Int = DEFAULT_CHAR_BUFFER_SIZE) : Reader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

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,

{@snippet lang=java :

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

Author

Mark Reinhold

Since

1.1

See also

FileReader
java.nio.file.Files.newBufferedReader

Inheritors

Constructors

Link copied to clipboard
constructor(in: Reader, sz: Int = DEFAULT_CHAR_BUFFER_SIZE)

Types

Link copied to clipboard
object Companion

Returns a Stream, the elements of which are lines read from this BufferedReader. The Stream is lazily populated, i.e., read only occurs during the ../util/stream/package-summary.html#StreamOps.

Functions

Link copied to clipboard
open override fun close()

Closes the reader and releases any associated resources.

Link copied to clipboard
open fun mark(readAheadLimit: Int)

Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point.

Link copied to clipboard

Tells whether this stream supports the mark() operation, which it does.

Link copied to clipboard
open override fun read(): Int

Reads a single character.

open override fun read(cbuf: CharArray, off: Int, len: Int): Int

Reads characters into a portion of an array.

open override fun read(target: CharBuffer): Int

Reads characters into a CharBuffer. This default implementation reads into an array and then puts the characters into the buffer.

Link copied to clipboard
open fun readLine(): String?
fun readLine(ignoreLF: Boolean, term: BooleanArray?): String?

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), a carriage return followed immediately by a line feed, or by reaching the end-of-file (EOF).

Link copied to clipboard
open override fun ready(): Boolean

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

Link copied to clipboard
open override fun reset()

Resets the stream to the most recent mark.

Link copied to clipboard
open fun skip(n: Long): Long

{@inheritDoc}

Link copied to clipboard
open fun transferTo(out: Writer): Long

Reads all characters from this Reader and writes them to out. Returns the number of characters transferred.