File to byte[] in Java

2021-6-3 anglehua

How do I convert a to a byte[]?

It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. Which is here FileUtils.readFileToByteArray(File input).

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


import java.nio.file.Files;

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


Powered by emlog 京ICP备15036472号-3 sitemap