缓冲流进行复制文件

public class CopyDemo2 {
		
	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("API.rar");
		   BufferedInputStream bis=new BufferedInputStream(fis);
		
		FileOutputStream fos=new FileOutputStream("API02.rar");
		
		 BufferedOutputStream bos=new BufferedOutputStream(fos);
		   
		//byte[] data=new byte[1024*10];
		 long start=System.currentTimeMillis();
		int d=-1;
		/*
		 * 缓冲流内部维护着一个缓冲区(字节数组)
		 * bis.read()看似读取一个字节,实际上缓冲流会一次性通过fis读取一组字节,
		 * 并存入内部维护的字节数组中,然后将第一个字节返回。
		 * 这样当再次调用bis.read()读取一个字节时,会直接从内部的字节数组中将第二个字节返回
		 * 所以缓冲流还是通过提高一次性实际读取的字节量
		 * 减少实际读取次数提高的效率
		 * 缓冲输出流也是类似的效率
		 */
		while((d=bis.read())!=-1){
		//fos.write(data,0,len);
			bos.write(d);
		}
		System.out.println("共耗时:"+(System.currentTimeMillis()-start)+"ms");
		System.out.println("复制完毕");
		/*
		 * 关闭流只需要关闭最外层高级流即可
		 */
	    bis.close();
	    bos.close();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值