造轮子
private byte[] fileConvertToByteArray(File file) {
byte[] data;
try (InputStream inputStream = Files.newInputStream(file.toPath());
BufferedInputStream bis = new BufferedInputStream(inputStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
int len;
byte[] buffer = new byte[1024];
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
data = bos.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
return data;
}
使用轮子
private byte[] readAllBytes(String fileName) throws IOException {
return Files.readAllBytes(Paths.get(fileName));
}