首页 新闻 会员 周边

BufferedInputStream 和BufferedOutputStream

0
悬赏园豆:50 [已关闭问题] 关闭于 2016-11-30 17:22

文件上传:

代码1:

InputStream is = mFile.getInputStream();//mFile为MultipartFile实例
File destFile = new File(savePath, newName);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
is.close();
os.close();

 

代码2:

InputStream is = mFile.getInputStream();//mFile为MultipartFile实例
File destFile = new File(savePath, newName);
OutputStream os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
BufferedInputStream bis = new BufferedInputStream(is);//添加缓存
BufferedOutputStream bos = new BufferedOutputStream(os);
while ((length = bis.read(buffer)) > 0) {
bos.write(buffer, 0, length);
}
is.close();
os.close();

第一种写法也是一块一块的读入、输出的,我以为就相当于是进行了缓冲;采用第二种写法速度明显提高,哪位大神可以解释一下两种方法的运行原理,在此谢过!

薄荷香茶的主页 薄荷香茶 | 初学一级 | 园豆:48
提问于:2015-04-30 16:12
< >
分享
所有回答(1)
0

比喻一下

第一种:A 到 B,一路都是骑单车,不断往返A和B

第二种:A 到 B,在A和B之间,引入了C,A到C依然是骑单车,而C到B,就不是骑单车了,而是开小车,总体效率就高了

Yu | 园豆:12980 (专家六级) | 2015-05-01 13:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册