In this tutorial, you will learn how to read a file as byte array in Java, using readAllBytes() method of java.nio.file.Files class.
Java – Read file as byte array
To read a file as byte array in Java, given a file path, call readAllBytes() method of the java.nio.file.Files class and pass the file path as argument to the method. The readAllBytes() method returns a byte array with the bytes of the file.
If filePath is the given file path, then use the following expression to get the bytes in file.
</>
Copy
Files.readAllBytes(filePath)
1. Read the file “data.txt” as byte array in Java
In the following program, we take a file path "/Users/tutorialkart/data.txt", read the content of this file as byte array, and print them to standard output.
