From 362610ca587c741843e616d387f613acad3c1340 Mon Sep 17 00:00:00 2001 From: Azhwani <13301425+azhwani@users.noreply.github.com> Date: Tue, 7 Nov 2023 19:21:46 +0100 Subject: [PATCH] BAEL-6776: Convert File to Byte Array in Java (#15123) --- .../FileToByteArrayUnitTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/filetofrombytearray/FileToByteArrayUnitTest.java diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/filetofrombytearray/FileToByteArrayUnitTest.java b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/filetofrombytearray/FileToByteArrayUnitTest.java new file mode 100644 index 0000000000..1bbd958583 --- /dev/null +++ b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/filetofrombytearray/FileToByteArrayUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.filetofrombytearray; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.api.Test; + +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); + } + + @Test + void givenFile_whenUsingNioApiFilesClass_thenConvert() throws IOException { + byte[] byteArray = Files.readAllBytes(Paths.get(FILE_NAME)); + + assertArrayEquals(expectedByteArray, byteArray); + } + + @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); + } + + @Test + void givenFile_whenUsingApacheCommonsFileUtilsClass_thenConvert() throws IOException { + byte[] byteArray = FileUtils.readFileToByteArray(new File(FILE_NAME)); + + assertArrayEquals(expectedByteArray, byteArray); + } + + @Test + void givenFile_whenUsingGuavaFilesClass_thenConvert() throws IOException { + byte[] byteArray = com.google.common.io.Files.toByteArray(new File(FILE_NAME)); + + assertArrayEquals(expectedByteArray, byteArray); + } + +}