public static void convertByteArrayToFile(byte[] arr) {
try (
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(arr));
FileOutputStream fileOutputStream = new FileOutputStream("D:\\test\\test.class");
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream)
) {
int data;
while ((data = bis.read()) != -1) {
bos.write(data);
}
bos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] convertFileToByteArray(String fileName) throws IOException {
BufferedInputStream bis;
ByteArrayOutputStream bos;
bis = new BufferedInputStream(new FileInputStream(fileName));
bos = new ByteArrayOutputStream();
int data;
while ((data = bis.read()) != -1) {
bos.write(data);
}
bos.flush();
return bos.toByteArray();
}
用处:可以将远程加载进来的字节流转换为本地的文件保存;