使用Java的FileInputStream
在java.io.FileInputStream
中,有一个方法int read(Byte[] buffer,int offset,int numBytes)
; 我们如何使用这个函数 - 这个方法和 read(byte[] buffer) 之间有什么区别吗?
In java.io.FileInputStream
, there is a method int read(Byte[] buffer,int offset,int numBytes)
; how we can use this function - is there any difference between this method and read(byte[] buffer)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 Javadoc 指出的(以及参数名称所示),带有 offset 和 numBytes 的方法仅使用缓冲区的一部分来放入其输出。
如果您想重用已包含数据的现有缓冲区,则可以使用此方法您不想破坏其中的内容(当然,从
offset
开始的numBytes
将被覆盖)。在Java中,几乎所有对缓冲区的操作都提供这种接口。 如果使用得当,您可以避免不必要地多次复制/缓冲数据。
As the Javadoc points out (and the names of the parameters indicate), the method with offset and numBytes uses only part of the buffer to put its output in.
You can use this method if you want to reuse an existing buffer that already has data in it that you do not want to clobber (Of course, the
numBytes
starting fromoffset
will get overwritten).In Java, almost all operations on buffers offer this kind of interface. Used properly, you can avoid copying/buffering data more times than necessary.
刚刚从 javadoc 得到这个。
从此输入流中读取最多 len 个字节的数据到字节数组中。 如果 len 不为零,则该方法将阻塞,直到某些输入可用为止; 否则,不会读取任何字节并返回 0。
参数:
返回:
读入缓冲区的总字节数,如果由于已到达文件末尾而没有更多数据,则为 -1。
http://java. sun.com/javase/6/docs/api/java/io/FileInputStream.html#read(byte[], int, int)
Just got this from the javadoc.
Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.
Parameters:
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
http://java.sun.com/javase/6/docs/api/java/io/FileInputStream.html#read(byte[], int, int)
该函数对于将整个文件读入内存非常有用。 看这个例子,
This function is very useful to read a whole file into memory. See this example,
http://java.sun.com/docs/books/教程/essential/io/index.html
http://java.sun.com/docs/books/tutorial/essential/io/index.html