简体   繁体   English

从类Reader读取(char [] cbuf,int off,int len)

[英]Confusion on read(char[] cbuf, int off, int len) from class Reader

To my understanding, this abstract method reads characters into a portion of an array. 据我所知,这个抽象方法将字符读入数组的一部分。

What confuses me is that it returns the number of character read, or -1 if it reaches the end of stream. 令我困惑的是它返回读取的字符数,如果它到达流的末尾,则返回-1。

Does that mean that the read method read 1 character at a time and return the no. 这是否意味着read方法一次读取1个字符并返回no。 of character read, and then once it hits the end it returns -1? 字符读取,然后一旦它到达结束它返回-1?

Eg, if it's reading a string "abcd" form a txt file with read(cbuf, 0 4), it returns 1234 and finally -1? 例如,如果它正在读取字符串“abcd”形成一个带有read(cbuf,0 4)的txt文件,它返回1234,最后返回-1?

And if that's the case, does that mean we can only access the read characters through cbuf? 如果是这样的话,这是否意味着我们只能通过cbuf访问读取的字符?

edit2: Thanks again everyone, I finally figured out everything. edit2:再次感谢大家,我终于弄清楚了一切。 Cheers! 干杯!

In general, the read method (from various classes) will read up to a maximum of len characters in one operation. 通常,read方法(来自各种类)将在一次操作中读取最多len字符。 It might read fewer characters. 它可能会读更少的字符。 You should check the return value of the function to know how many such characters it read. 您应该检查函数的返回值,以了解它读取的字符数。

Once it reaches the EOF, it will return -1. 一旦达到EOF,它将返回-1。

Take a look at the Documentation of BufferedReader.read : 看一下BufferedReader.read文档

This method implements the general contract of the corresponding read method of the Reader class. 此方法实现Reader类的相应读取方法的常规协定。 As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read method of the underlying stream. 作为额外的便利,它尝试通过重复调用底层流的读取方法来读取尽可能多的字符。 This iterated read continues until one of the following conditions becomes true: 此迭代读取将继续,直到满足以下条件之一:

  1. The specified number of characters have been read 已读取指定的字符数
  2. The read method of the underlying stream returns -1, indicating end-of-file. 底层流的read方法返回-1,表示文件结束。
  3. The ready method of the underlying stream returns false, indicating that further input requests would block. 底层流的ready方法返回false,表示进一步的输入请求将被阻塞。

... ...

If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. 如果对基础流的第一次读取返回-1以指示文件结束,则此方法返回-1。 Otherwise this method returns the number of characters actually read. 否则,此方法返回实际读取的字符数。

.... ....

Ordinarily this method takes characters from this stream's character buffer, filling it from the underlying stream as necessary. 通常,此方法从此流的字符缓冲区中获取字符,并根据需要从基础流中填充它。 If, however, the buffer is empty, the mark is not valid, and the requested length is at least as large as the buffer, then this method will read characters directly from the underlying stream into the given array. 但是,如果缓冲区为空,则标记无效,并且请求的长度至少与缓冲区一样大,则此方法将直接从基础流读取字符到给定数组中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 read(char[] cbuf, int off, int len) 读取整个文件 - How to read whole file with read(char[] cbuf, int off, int len) InputStream中的read(byte b[], int off, int len)方法和FileInputStream中的read(byte b[], int off, int len)方法一样吗? - Is the method read(byte b[], int off, int len) in InputStream same as the method read(byte b[], int off, int len) in FileInputStream? BufferedInputStream.read(byte [] b,int off,int len)中的off参数 - The off parameter in BufferedInputStream.read(byte[] b, int off, int len) read(byte [] b,int off,int len):读取文件到最后 - read(byte[] b, int off, int len) : Reading A File To Its End JAVA:InputStream.read(byte [] b,int off,int len)中的字节数组分配 - JAVA: Byte array allocation in InputStream.read(byte[] b, int off, int len) InputStream read(byte [] b,int off,int len)-删除文件的其余部分? - InputStream read(byte[] b, int off, int len) - Drop the rest of the file? Java char / int转换混乱 - Java char/int conversion confusion readFully(byte [] b,int off,int len)和EOFException - readFully(byte[] b, int off, int len) and EOFException Can BufferedInputStream.read(byte [] b,int off,int len)是否会返回0? 是否有重要的,破坏的InputStream可能会导致这种情况? - Can BufferedInputStream.read(byte[] b, int off, int len) ever return 0? Are there significant, broken InputStreams that might cause this? BufferedReader read(char [],int,int)是否被阻止? - BufferedReader read(char[], int, int) blocked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM