How do I convert a java.io.File
to a byte[]
?
File to byte[] in Java
2021-6-3 anglehua
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());
采集自互联网,如有侵权请联系本人