您的位置:首页 > 编程语言 > Java开发

IO-缓冲流的使用。

2016-06-14 20:32 375 查看
缓冲流的作用:

频繁读写效率太低,硬盘效率太低。减少对硬盘的读写次数。

原理:

先将要读写的内容先都缓冲起来,最后一起处理。

下面是一个视频复制的例子。

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java