Convert File to Byte Array in Java – 用 Java 将文件转换为字节数组

最后修改: 2023年 11月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll see how to convert a file to a byte array in Java.

在本快速教程中,我们将了解如何在 Java 中将文件转换为字节数组。

First, we’ll learn how to do it using built-in JDK solutions. Then, we’ll discuss how to achieve the same result using Apache Commons IO and Guava.

首先,我们将学习如何使用 JDK 内置解决方案来实现这一目标。然后,我们将讨论如何使用 Apache Commons IO 和 Guava 实现同样的结果。

2. Using Java

2.使用 Java

JDK provides several convenient ways to convert a file into an array of bytes. For example, we can use the java.io or java.nio packages to answer our central question. So, let’s take a close look at each option.

JDK 提供了几种将文件转换为字节数组的便捷方法。例如,我们可以使用 java.iojava.nio 包来回答我们的中心问题。因此,让我们仔细研究一下每个选项。

2.1. FileInputStream

2.1.文件输入流</em

Let’s start with the easiest solution using the FileInputStream class from the IO package. Typically, this class comes with methods to read the content of a file as bytes.

让我们从最简单的解决方案开始,使用 IO 包中的 FileInputStream 类。通常情况下,该类带有以字节形式读取文件内容的方法。

For instance, let’s assume we have a file named sample.txt with the content “Hello World”:

例如,假设我们有一个名为sample.txt的文件,内容为“Hello World”

class FileToByteArrayUnitTest {

    private static final String FILE_NAME = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "sample.txt";
    private final byte[] expectedByteArray = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 };

    @Test
    void givenFile_whenUsingFileInputStreamClass_thenConvert() throws IOException {
        File myFile = new File(FILE_NAME);
        byte[] byteArray = new byte[(int) myFile.length()];
        try (FileInputStream inputStream = new FileInputStream(myFile)) {
            inputStream.read(byteArray);
        }

        assertArrayEquals(expectedByteArray, byteArray);
    }
}

Here, we created an instance of the FileInputStream class using the given sample.txt file. Furthermore, we invoked the read(byte[] b) method to read the data from the FileInputStream instance into the defined array of bytes.

在这里,我们使用给定的 sample.txt 文件创建了一个 FileInputStream 类实例。此外,我们还调用了 read(byte[] b) 方法,将数据从 FileInputStream 实例读取到定义的字节数组中。

It’s worth noting that we used the try-with-resources approach to handle the closure of the resources efficiently.

值得注意的是,我们使用了 try-with-resources 方法来高效处理资源关闭

2.2. Files#readAllBytes

2.2.文件#readAllBytes</em

Alternatively, we can use the Files class from the NIO API. As the name implies, this utility class offers multiple ready-to-use static methods for working with files and directories.

或者,我们可以使用 NIO API 中的 Files 类。顾名思义,这个实用程序类提供了多个随时可用的静态方法,用于处理文件和目录

So, let’s see it in action:

那么,让我们来看看它的实际效果吧:

@Test
void givenFile_whenUsingNioApiFilesClass_thenConvert() throws IOException {
    byte[] byteArray = Files.readAllBytes(Paths.get(FILE_NAME));

    assertArrayEquals(expectedByteArray, byteArray);
}

As we can see, the Files class comes with the readAllBytes() method which returns all the bytes from the specified path file. Interestingly, this method closes the file automatically when the bytes have been read.

正如我们所看到的,Files 类附带的 readAllBytes() 方法可返回指定路径文件中的所有字节。有趣的是,该方法会在字节读取完毕后自动关闭文件

Another important caveat here is that this method isn’t intended to read large files. So, we can use it only for simple cases.

另一个重要的注意事项是,此方法并非用于读取大文件。因此,我们只能在简单的情况下使用它。

3. Using Apache Commons IO

3.使用 Apache Commons IO

Another solution would be using the Apache Commons IO library. It offers a host of handy utility classes that we can use to perform common IO tasks.

另一种解决方案是使用 Apache Commons IO 库。它提供了大量方便的实用程序类,我们可以用它们来执行常见的 IO 任务。

First, let’s add the commons-io dependency to the pom.xml file:

首先,让我们将 commons-io 依赖关系添加到 pom.xml 文件中:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.15.1</version>
</dependency>

3.1. FileUtils#readFileToByteArray

3.1 FileUtils#readFileToByteArray

The FileUtils class, as the name indicates, provides a set of methods for file manipulation. Among these methods, we find the readFileToByteArray() method:

FileUtils 类,顾名思义,提供了一系列文件操作方法。在这些方法中,我们可以找到 readFileToByteArray() 方法:

@Test
void givenFile_whenUsingApacheCommonsFileUtilsClass_thenConvert() throws IOException {
    byte[] byteArray = FileUtils.readFileToByteArray(new File(FILE_NAME));

    assertArrayEquals(expectedByteArray, byteArray);
}

As we see above, readFileToByteArray() reads the content of the specified file into a byte array in a straightforward way. The good thing about this method is that the file is always closed.

如上所述, readFileToByteArray() 将指定文件的内容直接读入字节数组。该方法的优点是文件总是关闭的。

Also, this method doesn’t have the limitations of Files#readAllBytes and throws NullPointerException if the provided file is null.

此外,该方法没有 Files#readAllBytes 的限制,如果提供的文件是 null 文件,则会抛出 NullPointerException

3.2. IOUtils#toByteArray

3.2 IOUtils#toByteArray

Apache Commons IO provides another alternative that we can use to achieve the same outcome. It offers the IOUtils class to handle general IO stream operations.

Apache Commons IO 提供了另一种选择,我们可以用它来实现相同的结果。它提供了 IOUtils 类来处理一般的 IO 流操作。

So, let’s exemplify the use of IOUtils using a practical example:

因此,让我们用一个实际例子来说明 IOUtils 的使用:

@Test
void givenFile_whenUsingApacheCommonsIOUtilsClass_thenConvert() throws IOException {
    File myFile = new File(FILE_NAME);
    byte[] byteArray = new byte[(int) myFile.length()];
    try (FileInputStream inputStream = new FileInputStream(myFile)) {
        byteArray = IOUtils.toByteArray(inputStream);
    }

    assertArrayEquals(expectedByteArray, byteArray);
}

In short, the class comes with the toByteArray() method to return the data of an InputStream as a byte[]. We don’t need to use BufferedInputStream here since this method buffers the content internally.

简而言之,该类自带的 toByteArray() 方法可将 InputStream 的数据以 byte[] 的形式返回。在这里我们不需要使用 BufferedInputStream 方法,因为该方法会在内部对内容进行缓冲。

4. Using Guava

4.使用番石榴

Guava library is another option to consider when converting a file to a byte array. As usual, before starting to work with this library, we need to add its dependency to pom.xml:

Guava 库是将文件转换为字节数组时需要考虑的另一个选项。与往常一样,在开始使用该库之前,我们需要将其 依赖关系添加到 pom.xml 中:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>32.1.3-jre</version>
</dependency>

4.1. Files#toByteArray

4.1 Files#toByteArray 文件数组

The Guava library provides its own version of the Files class. So, let’s see it in practice:

Guava 库提供了自己版本的 Files 类。因此,让我们来看看它的实际应用:

@Test
void givenFile_whenUsingGuavaFilesClass_thenConvert() throws IOException {
    byte[] byteArray = com.google.common.io.Files.toByteArray(new File(FILE_NAME));

    assertArrayEquals(expectedByteArray, byteArray);
}

In a nutshell, we use the toByteArray() method to get a byte array containing all the bytes from the given file.

简而言之,我们使用 toByteArray() 方法获取包含给定文件中所有字节的字节数组

5. Conclusion

5.结论

In this short article, we explored various ways to convert a file to a byte array using JDK methods, Guava, and Apache Commons IO libraries.

在这篇短文中,我们探讨了使用 JDK 方法、Guava 和 Apache Commons IO 库将文件转换为字节数组的各种方法。

As always, the code used in this article can be found over on GitHub.

与往常一样,本文中使用的代码可以在 GitHub 上找到