File to byte[] in Java

2021-6-3 anglehua

How do I convert a java.io.File 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).

Example:

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

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


采集自互联网,如有侵权请联系本人

Powered by emlog 京ICP备15036472号-3 sitemap