Class BufferedReader

java.lang.Object
java.io.Reader
java.io.BufferedReader
All Implemented Interfaces:
Closeable, AutoCloseable

public class BufferedReader extends Reader

Reads text from another reader through a buffer, and reads it a line at a time. A line ends at \n, \r or \r\n.

Like most readers, it is for one thread at a time.

  • Field Summary

    Fields inherited from class Reader

    lock
  • Constructor Summary

    Constructors
    Constructor
    Description
    Buffers in with an 8192 character buffer.
    BufferedReader(Reader in, int size)
    Buffers in with a buffer of size characters.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the stream.
    The remaining lines, read as the stream asks for them.
    void
    mark(int readAheadLimit)
    Mark the present position in the stream.
    boolean
    Tell whether this stream supports the mark() operation.
    int
    Read a single character.
    int
    read(char[] cbuf, int off, int len)
    Read characters into a portion of an array.
    Reads a line of text, without its terminator.
    boolean
    Tell whether this stream is ready to be read.
    void
    Reset the stream.
    long
    skip(long n)
    Skip characters.

    Methods inherited from class Reader

    read, transferTo

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BufferedReader

      public BufferedReader(Reader in)
      Buffers in with an 8192 character buffer.
    • BufferedReader

      public BufferedReader(Reader in, int size)

      Buffers in with a buffer of size characters.

      Throws
      • IllegalArgumentException: when size is not positive
  • Method Details

    • read

      public int read() throws IOException
      Description copied from class: Reader
      Read a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached. Subclasses that intend to support efficient single-character input should override this method.
      Overrides:
      read in class Reader
      Throws:
      IOException
    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      Description copied from class: Reader
      Read characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.
      Specified by:
      read in class Reader
      Throws:
      IOException
    • readLine

      public String readLine() throws IOException

      Reads a line of text, without its terminator.

      Returns

      the line, or null at the end of the text

      Throws:
      IOException
    • ready

      public boolean ready() throws IOException
      Description copied from class: Reader
      Tell whether this stream is ready to be read.
      Overrides:
      ready in class Reader
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Description copied from class: Reader
      Skip characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.
      Overrides:
      skip in class Reader
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
      Description copied from class: Reader
      Tell whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.
      Overrides:
      markSupported in class Reader
    • mark

      public void mark(int readAheadLimit) throws IOException
      Description copied from class: Reader
      Mark the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark() operation.
      Overrides:
      mark in class Reader
      Throws:
      IOException
    • reset

      public void reset() throws IOException
      Description copied from class: Reader
      Reset the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().
      Overrides:
      reset in class Reader
      Throws:
      IOException
    • close

      public void close() throws IOException
      Description copied from class: Reader
      Close the stream. Once a stream has been closed, further read(), ready(), mark(), or reset() invocations will throw an IOException. Closing a previously-closed stream, however, has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader
      Throws:
      IOException
    • lines

      public Stream<String> lines()
      The remaining lines, read as the stream asks for them. A read that fails surfaces as an UncheckedIOException; closing the stream does not close this reader, as in the JDK.