From JDK 7 you can use Files.readAllBytes(Path).

Example:

Copyimport java.io.File;
import java.nio.file.Files;

File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());
🌐
Baeldung
baeldung.com › home › java › java io › convert file to byte array in java
Convert File to Byte Array in Java | Baeldung
January 5, 2024 - 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.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
7 Examples to Read File into a byte array in Java - Java Code Geeks
April 27, 2020 - Unfortunately, Java’s File class, which is used to represent both files and directories, doesn’t have a method say toByteArray(). It only holds path and allows you to perform certain operations like opening and closing file, but doesn’t allow you to directly convert File to a byte array. Anyway, no need to worry as there are several other ways to read File into a byte array and you will learn those in this Java file tutorial.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-file-to-byte-array-and-vice-versa
Java Program to Convert File to byte array and Vice-Versa | Vultr Docs
December 5, 2024 - This method makes use of the NIO package to read all bytes from the file at once. This is generally a cleaner and more efficient way to handle file data conversion to byte arrays. Once a file is converted to a byte array, you might need to convert it back to a file format. Here's how to accomplish this using Java...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-file-to-a-byte-array
Java Program to Convert File to a Byte Array - GeeksforGeeks
July 23, 2025 - For reading streams of characters, consider using FileReader. read(byte[]) method of FileInputStream class which reads up to the length of the file and then converts bytes of data from this input stream into the byte array.
🌐
How to do in Java
howtodoinjava.com › home › i/o › read file to byte[] in java
Read File to Byte[] in Java
December 14, 2022 - The following example uses the com.google.common.io.Files class to read the file content into a byte array. byte[] bytes3 = com.google.common.io.Files.toByteArray(file); Happy Learning !!
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › io › fileinputstream
Read file in byte array with FileInputStream - Java Code Geeks
October 26, 2013 - Use read(byte[] b) API method of FileInputStream to read up to certain bytes of data from this input stream into the byte array. Create a String from the byte array. Don’t forget to close the FileInputStream, using the close() API method.
🌐
Mkyong
mkyong.com › home › java › java – how to convert file to byte[]
Java - How to convert File to byte[] - Mkyong.com
September 17, 2020 - In Java, we can use `Files.readAllBytes(path)` to convert a `File` object into a `byte[]`.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 276442 › java › Reading-file-byte-array
Reading file into byte array (I/O and Streams forum at Coderanch)
December 12, 2003 - But what should happen is, that, it enters into another loop if the file is not read until the end, what is not done here, but that what's described. The method read returns integer, quote from javadoc: Reads up to len bytes of data from the input stream into an array of bytes.
🌐
Netjstech
netjstech.com › 2015 › 11 › how-to-convert-file-to-byte-array-java.html
Java Program to Convert a File to Byte Array | Tech Tutorials
You can use java.io.FileInputStream to read file content into a byte array using the read() method.
🌐
Blogger
javarevisited.blogspot.com › 2020 › 04 › 7-examples-to-read-file-into-byte-array-in-java.html
7 Examples to Read File into a Byte Array in Java
July 28, 2021 - Unfortunately, Java's File class, which is used to represent both files and directories, doesn't have a method say toByteArray(). It only holds path and allows you to perform certain operations like opening and closing file, but doesn't allow you to directly convert File to a byte array. Anyway, no need to worry as there are several other ways to read File into a byte array and you will learn those in this Java file tutorial.
🌐
Coderanch
coderanch.com › t › 277312 › java › read-file-data-byte-array
how to read a file data into byte array (I/O and Streams forum at Coderanch)
This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() ... Hi Right now I am using the following code to read the data from file into a StringBuffer.
🌐
Level Up Lunch
leveluplunch.com › java › examples › convert-file-to-byte-array
File to byte array | Level Up Lunch
October 28, 2013 - @Test public void file_to_byte_array_java () throws IOException { File file = new File(fileLocation); byte[] fileInBytes = new byte[(int) file.length()]; InputStream inputStream = null; try { inputStream = new FileInputStream(file); inputStream.read(fileInBytes); } finally { inputStream.close(); } assertEquals(18, fileInBytes.length); }
🌐
Programiz
programiz.com › java-programming › examples › convert-file-byte-array
Java Program to Convert File to byte array and Vice-Versa
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; public class FileByte { public static void main(String[] args) { String path = System.getProperty("user.dir") + "\\src\\test.txt"; try { byte[] encoded = Files.readAllBytes(Paths.get(path)); System.out.println(Arrays.toString(encoded)); } catch (IOException e) { } } }
🌐
w3resource
w3resource.com › java-exercises › io › java-io-exercise-10.php
Java - Read contents from a file into byte array
May 19, 2025 - import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; // Reading contents from a file into byte array.
🌐
YouTube
youtube.com › watch
How to read a file in byte array in Java? - YouTube
In this video tutorial you will learn How to read a file in byte array in Java? In our example we are reading the text file into byte and also printing the d...
Published   January 4, 2016
🌐
TutorialKart
tutorialkart.com › java › java-read-file-as-bytes
Java - Read file as byte array
August 23, 2023 - The readAllBytes() method returns byte array created from the content of the file. Store the returned value in a variable fileBytes. Iterate over the byte array using a Java For loop, and print the bytes in byte array to standard output.
🌐
Oracle
forums.oracle.com › ords › apexds › post › fastest-way-of-reading-a-binary-file-into-a-byte-array-9966
Fastest way of reading a binary file into a byte array - Oracle Forums
June 5, 2007 - Hi I have seen there are alot of methods for reading files in Java, and in this jungle I have gotten a bit confused on which way is better to solve a specific problem. MY problem in this case is to r...
🌐
PREP INSTA
prepinsta.com › home › java tutorial › java program to convert file to byte array and vice-versa
Java Program to Convert File to byte array and Vice-Versa
May 23, 2023 - Create a byte array with the file’s length in it. To read the file’s contents into the byte array, use the read() function of the FileInputStream object.