缓冲流的作用:
频繁读写效率太低,硬盘效率太低。减少对硬盘的读写次数。
原理:
先将要读写的内容先都缓冲起来,最后一起处理。
下面是一个视频复制的例子。
InputStream is = null;
BufferedInputStream bis = null;
OutputStream os = null;
BufferedOutputStream bos = null;
try {
is = new FileInputStream("E:/英雄时刻/英雄时刻_20160604-00点11分36s.avi");
bis = new BufferedInputStream(is);
os = new FileOutputStream("e:/英雄时刻_20160604-00点11分36s.avi");
bos = new BufferedOutputStream(os);
byte[] buf = new byte[32];
int len = -1;
while ((len=bis.read(buf))!=-1){
bos.write(buf,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bos.close();
os.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}