【问题标题】:How to create ByteArrayInputStream from a file in Java?如何从 Java 中的文件创建 ByteArrayInputStream?
【发布时间】:2012-06-28 17:55:35
【问题描述】:

我有一个文件,可以是 ZIP、RAR、txt、CSV、doc 等任何文件。我想从中创建一个 ByteArrayInputStream
我正在使用它通过 FTPClient 从 Apache Commons Net 将文件上传到 FTP。

有人知道怎么做吗?

例如:

String data = "hdfhdfhdfhd";
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());

我的代码:

public static ByteArrayInputStream retrieveByteArrayInputStream(File file) {
    ByteArrayInputStream in;

    return in;     
}

【问题讨论】:

  • 对于以字节为单位的文件读取,我使用 RandomAccessFile 并首先将整个文件字节传输到一个字节数组中。我发现这是一种以字节为单位读取文件的非常快速的方法。
  • 您为什么需要这样做?您可以通过将 FileInputStream 复制到 ByteArrayOutputStream 并从中创建 ByteArrayInputStream 来做到这一点。当然,这是毫无意义的。
  • 请解释您的用例。您可能只需要一个 FileInputStream。拥有它后,您打算如何使用它?
  • 我承认这有点不必要,但我发现它是读取文件的最快方法之一,如果你喜欢速度的话。然后我又知道只使用 C...
  • 这似乎比从FileInputStream 读取要严格慢,不是吗?

标签: java arrays bytebuffer bytearrayinputstream


【解决方案1】:

使用Apache Commons IO 中的FileUtils#readFileToByteArray(File),然后使用ByteArrayInputStream(byte[]) 构造函数创建ByteArrayInputStream

public static ByteArrayInputStream retrieveByteArrayInputStream(File file) {
    return new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
}

【讨论】:

  • 如我的回答中所述,Java 7 已经在 Files 类中包含了 readFileToByteArray,不需要额外的库。
【解决方案2】:

一般的想法是一个文件会产生一个FileInputStream和一个byte[]一个ByteArrayInputStream。两者都实现了InputStream,因此它们应该与使用InputStream 作为参数的任何方法兼容。

当然可以将所有文件内容放在ByteArrayInputStream 中:

  1. 将完整文件读入byte[]; Java 版本 >= 7 包含一个 convenience method called readAllBytes 用于从文件中读取所有数据;
  2. 围绕文件内容创建一个ByteArrayInputStream,该文件现在在内存中。

请注意,对于非常大的文件,这可能不是最佳解决方案 - 所有文件将同时存储在内存中。为工作使用正确的流很重要。

【讨论】:

  • 小记:Java还包含内存映射文件的方式,如果需要进行随机读写可能会更有利。从文件流式传输数据不是唯一的选择,也不是最好的选择。
【解决方案3】:

ByteArrayInputStream 是一个围绕字节数组的 InputStream 包装器。这意味着您必须将文件完全读入byte[],然后使用ByteArrayInputStream 构造函数之一。

您能否详细说明您使用ByteArrayInputStream 所做的事情?可能有更好的方法来解决您想要实现的目标。

编辑:
如果你使用 Apache FTPClient 上传,你只需要一个InputStream。你可以这样做;

String remote = "whatever";
InputStream is = new FileInputStream(new File("your file"));
ftpClient.storeFile(remote, is);

您当然应该记住在完成后关闭输入流。

【讨论】:

  • 我正在使用它通过 FTPClient commons Apache 将文件上传到 ftp。
  • 我会建议the try-with-resources Statement 而不是“记得关闭输入流”。
【解决方案4】:

这段代码很方便:

private static byte[] readContentIntoByteArray(File file)
{
  FileInputStream fileInputStream = null;
  byte[] bFile = new byte[(int) file.length()];
  try
  {
     //convert file into array of bytes
     fileInputStream = new FileInputStream(file);
     fileInputStream.read(bFile);
     fileInputStream.close();
  }
  catch (Exception e)
  {
     e.printStackTrace();
  }
  return bFile;
}

参考:http://howtodoinjava.com/2014/11/04/how-to-read-file-content-into-byte-array-in-java/

【讨论】:

    【解决方案5】:

    这并不是您所要求的,而是一种以字节为单位读取文件的快速方法。

    File file = new File(yourFileName);
    RandomAccessFile ra = new RandomAccessFile(yourFileName, "rw"):
    byte[] b = new byte[(int)file.length()];
    try {
        ra.read(b);
    } catch(Exception e) {
        e.printStackTrace();
    }
    
    //Then iterate through b
    

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多