- ByteArrayInputStream:将字节数组转成字节输入流
- ByteArrayOutputStream:从字节数组输出流可以获取字节数组
// 测试文件大小:2487KB
// 文件转为字节数组
public static byte[] ioOption() throws IOException {
// 缓冲字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:/demo.txt")));
// 字节数组输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes))!=-1){
baos.write(bytes,0,len);
}
bis.close();
baos.close();
return baos.toByteArray();
}
输出:
耗时(毫秒):8
// 测试文件大小:2487KB
// 字节数组转为文件
public static void ioOption(byte[] bytes) throws IOException {
// 字节数组输入流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);