使用Java的FileInputStream

发布于 2024-07-30 06:31:24 字数 162 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你げ笑在眉眼 2024-08-06 06:31:24

正如 Javadoc 指出的(以及参数名称所示),带有 offset 和 numBytes 的方法仅使用缓冲区的一部分来放入其输出。

public int read(byte[] b,
            int off,
            int len)
     throws IOException

Parameters:
    b - the buffer into which the data is read.
    off - the start offset of the data.
    len - the maximum number of bytes read. 

如果您想重用已包含数据的现有缓冲区,则可以使用此方法您不想破坏其中的内容(当然,从 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.

public int read(byte[] b,
            int off,
            int len)
     throws IOException

Parameters:
    b - the buffer into which the data is read.
    off - the start offset of the data.
    len - the maximum number of bytes read. 

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 from offset 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.

锦爱 2024-08-06 06:31:24

刚刚从 javadoc 得到这个。

从此输入流中读取最多 len 个字节的数据到字节数组中。 如果 len 不为零,则该方法将阻塞,直到某些输入可用为止; 否则,不会读取任何字节并返回 0。

参数:

  • b - 读取数据的缓冲区。
  • off - 目标数组 b 中的起始偏移量
  • len - 读取的最大字节数。

返回:
读入缓冲区的总字节数,如果由于已到达文件末尾而没有更多数据,则为 -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:

  • b - the buffer into which the data is read.
  • off - the start offset in the destination array b
  • len - the maximum number of bytes read.

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)

月棠 2024-08-06 06:31:24

该函数对于将整个文件读入内存非常有用。 看这个例子,

File = new File("/anywhere/anyfile");
InputStream is = new FileInputStream(file);
long fileSize = file.length();
byte[] bytes = new byte[(int)fileSize];
int offset = 0;
int count=0; 
while (offset < fileSize) {
    count=is.read(bytes, offset, fileSize-offset));
    if (count >= 0)
        offset += count;
    else
        throw new IOException("Can't read file "+file.getName());
}
is.close();
// Now bytes has all the complete file. 

This function is very useful to read a whole file into memory. See this example,

File = new File("/anywhere/anyfile");
InputStream is = new FileInputStream(file);
long fileSize = file.length();
byte[] bytes = new byte[(int)fileSize];
int offset = 0;
int count=0; 
while (offset < fileSize) {
    count=is.read(bytes, offset, fileSize-offset));
    if (count >= 0)
        offset += count;
    else
        throw new IOException("Can't read file "+file.getName());
}
is.close();
// Now bytes has all the complete file. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文