转自
public static byte[] fileToByteArray(String srcPath) {
File src = new File(srcPath);
byte[] datas = null;
try (InputStream is = new BufferedInputStream(new FileInputStream(src));
ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);
}
baos.flush();
datas = baos.toByteArray();
return datas;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void byteArrayToFile(byte[] datas, String destPath) {
File dest = new File(destPath);
try (InputStream is = new ByteArrayInputStream(datas);
OutputStream os = new BufferedOutputStream(new FileOutputStream(dest, false));) {
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}