将file文件转换成Byte数组

2年前 (2022) 程序员胖胖胖虎阿
243 0 0

/**
* 将file文件转换成Byte数组
* @param file 转换文件
* @return Byte数组
*/
public static byte[] getBytesByFile(File file) throws IOException {
FileInputStream fis = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
try {
fis = new FileInputStream(file);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
byte[] data = bos.toByteArray();
return data;
} catch (Exception e) {
log.error(“将文件转换成Byte数组失败”, e);
} finally {
if (fis != null){
fis.close();
}
bos.close();
}
return null;
}

版权声明:程序员胖胖胖虎阿 发表于 2022年10月20日 上午7:48。
转载请注明:将file文件转换成Byte数组 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...