Most visited

Recently visited

Added in API level 1

BufferedReader

public class BufferedReader
extends Reader

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


从字符输入流中读取文本,缓冲字符以提供字符,数组和行的高效读取。

缓冲区大小可以被指定,或者可以使用默认大小。 默认值对于大多数目的而言足够大。

通常,由Reader构成的每个读取请求都会导致相应的读取请求由底层字符或字节流组成。 因此,建议将BufferedReader包装在read()操作可能代价高昂的Reader中,如FileReaders和InputStreamReaders。 例如,

 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.

使用DataInputStreams进行文本输入的程序可以通过用适当的BufferedReader替换每个DataInputStream来进行本地化。

也可以看看:

Summary

Inherited fields

From class java.io.Reader

Public constructors

BufferedReader(Reader in, int sz)

创建使用指定大小的输入缓冲区的缓冲字符输入流。

BufferedReader(Reader in)

创建使用默认大小输入缓冲区的缓冲字符输入流。

Public methods

void close()

关闭流并释放与其关联的所有系统资源。

Stream<String> lines()

返回 Stream ,其中的元素是从此 BufferedReader读取的行。

void mark(int readAheadLimit)

标记流中的当前位置。

boolean markSupported()

告诉这个流是否支持mark()操作。

int read()

读取一个字符。

int read(char[] cbuf, int off, int len)

将字符读入数组的一部分。

String readLine()

读取一行文字。

boolean ready()

告诉这个流是否准备好被读取。

void reset()

将流重置为最近的标记。

long skip(long n)

跳过字符。

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, 
                int sz)

创建使用指定大小的输入缓冲区的缓冲字符输入流。

Parameters
in Reader: A Reader
sz int: Input-buffer size
Throws
IllegalArgumentException If sz is <= 0

BufferedReader

Added in API level 1
BufferedReader (Reader in)

创建使用默认大小输入缓冲区的缓冲字符输入流。

Parameters
in Reader: A Reader

Public methods

close

Added in API level 1
void close ()

关闭流并释放与其关联的所有系统资源。 一旦流被关闭,read(),ready(),mark(),reset()或skip()调用将会抛出一个IOException异常。 关闭以前关闭的流不起作用。

Throws
IOException

lines

Added in API level 24
Stream<String> lines ()

返回Stream ,其元素是从此BufferedReader读取的行。 StreamStream填充的,即只读发生在terminal stream operation期间。

在执行终端流操作期间,不得对阅读器进行操作。 否则,终端流操作的结果是未定义的。

在执行终端流操作之后,不能保证读者将处于读取下一个字符或行的特定位置。

如果IOException访问底层时被抛出BufferedReader ,它被包裹在一个UncheckedIOException其将从被抛出Stream方法导致所读取的发生。 如果在关闭的BufferedReader上调用该方法,该方法将返回一个Stream。 对该流进行的任何操作在关闭之后需要从BufferedReader读取时才会引发UncheckedIOException。

Returns
Stream<String> a Stream<String> providing the lines of text described by this BufferedReader

mark

Added in API level 1
void mark (int readAheadLimit)

标记流中的当前位置。 随后对reset()的调用将尝试将流重新定位到此点。

Parameters
readAheadLimit int: Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.
Throws
IllegalArgumentException If readAheadLimit is < 0
IOException If an I/O error occurs

markSupported

Added in API level 1
boolean markSupported ()

告诉这个流是否支持mark()操作。

Returns
boolean true if and only if this stream supports the mark operation.

read

Added in API level 1
int read ()

读取一个字符。

Returns
int The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
Throws
IOException If an I/O error occurs

read

Added in API level 1
int read (char[] cbuf, 
                int off, 
                int len)

将字符读入数组的一部分。

此方法实现Reader类的相应read方法的总体合同。 作为一个额外的便利,它尝试通过反复调用以读取尽可能多的字符可能read基础流的方法。 重复执行read直到下列其中一个条件成立为止:

  • The specified number of characters have been read,
  • The read method of the underlying stream returns -1, indicating end-of-file, or
  • The ready method of the underlying stream returns false, indicating that further input requests would block.
If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. Otherwise this method returns the number of characters actually read.

鼓励这个类的子类,但不是必需的,试图以相同的方式尽可能多地阅读字符。

通常,此方法从此流的字符缓冲区中获取字符,并根据需要从基础流中填充字符。 但是,如果缓冲区为空,标记无效,并且请求的长度至少与缓冲区一样大,那么此方法将直接从基础流读取字符到给定数组中。 因此多余的BufferedReader不会不必要地复制数据。

Parameters
cbuf char: Destination buffer
off int: Offset at which to start storing characters
len int: Maximum number of characters to read
Returns
int The number of characters read, or -1 if the end of the stream has been reached
Throws
IOException If an I/O error occurs

readLine

Added in API level 1
String readLine ()

读取一行文字。 换行符被换行符('\ n'),回车符('\ r')或回车符后面的换行符中的任何一个结束。

Returns
String A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws
IOException If an I/O error occurs

也可以看看:

ready

Added in API level 1
boolean ready ()

告诉这个流是否准备好被读取。 如果缓冲区不是空的,或者底层字符流已准备就绪,缓冲字符流就绪。

Returns
boolean True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Throws
IOException If an I/O error occurs

reset

Added in API level 1
void reset ()

将流重置为最近的标记。

Throws
IOException If the stream has never been marked, or if the mark has been invalidated

skip

Added in API level 1
long skip (long n)

跳过字符。

Parameters
n long: The number of characters to skip
Returns
long The number of characters actually skipped
Throws
IllegalArgumentException If n is negative.
IOException If an I/O error occurs

Hooray!