Java – File to byte[] in Java

file-iojava

How do I convert a java.io.File to a byte[]?

Best Solution

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

Example:

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

File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());