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.
🌐
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 !!
🌐
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[]`.
🌐
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.
🌐
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.
🌐
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.
🌐
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...
Find elsewhere
🌐
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.
🌐
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) { } } }
🌐
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_nio () throws IOException { Path path = Paths.get(fileLocation); byte[] fileInBytes = java.nio.file.Files.readAllBytes(path); assertEquals(18, fileInBytes.length); }
🌐
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.
🌐
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.
🌐
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.
🌐
RoseIndia
roseindia.net › java › javafile › java-read-binary-file-into-byte-array.shtml
Reading binary file into byte array in Java
import java.io.*; public class ... object File file = new File("test.zip"); //Instantiate the input stread InputStream insputStream = new FileInputStream(file); long length = file.length(); byte[] bytes = new byte[(int) length]; insputStream.read(bytes); insputStream.close(); ...
🌐
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.
🌐
Blogger
javarevisited.blogspot.com › 2020 › 04 › 7-examples-to-read-file-into-byte-array-in-java.html
Javarevisited: 7 Examples to Read File into a Byte Array in Java
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.
🌐
TutorialsPoint
tutorialspoint.com › How-to-write-contents-of-a-file-to-byte-array-in-Java
How to write contents of a file to byte array in Java?
December 19, 2019 - The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array. import java.io.File; import java.io.FileInputStream; public class FileToByteArray { public static void main(String args[]) ...