字节流四种方式复制文件,速度对比(图片和视频就用字节流)

实验最好用视频这个比较大的文件做对比,否则看不出效果
代码:

public class test {
	public static void main(String[] args) throws IOException {
		
		Method1("D:\\视频\\1.avi","C:\\Users\\29265\\Desktop\\1.avi");
		Method2("D:\\视频\\1.avi","C:\\Users\\29265\\Desktop\\2.avi");
		Method3("D:\\视频\\1.avi","C:\\Users\\29265\\Desktop\\3.avi");
		Method4("D:\\视频\\1.avi","C:\\Users\\29265\\Desktop\\4.avi");
		
	}

	private static void Method4(String string, String string2) throws IOException {
		// TODO Auto-generated method stub
		//获取开始时间
		long start=System.currentTimeMillis();
		
		//用BufferedInputStream一次读取一个字节数组
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream(string));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(string2));
		
		byte[] bt=new byte[1024];
		int len;
		while((len=bis.read(bt))!=-1){
			bos.write(bt,0,len);
		}
		//释放资源
		bis.close();
		bos.close();
		//获取结束时间
		long end=System.currentTimeMillis();
		 System.out.println("耗费时间:"+(end-start)+"毫秒!");
	}

	private static void Method3(String string, String string2) throws IOException {
		// TODO Auto-generated method stub
		//获取开始时间
		long start=System.currentTimeMillis();
		//用BufferedInputStream来一次读取一个字节
		//封装
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream(string));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(string2));
		
		
		while(bis.read()!=-1){
			bos.write(bis.read());
		}
		
		//释放资源
		bis.close();
		bos.close();
		//获取结束时间
		long end=System.currentTimeMillis();
		System.out.println("耗费时间:"+(end-start)+"毫秒!");
		
	}

	private static void Method2(String string, String string2) throws IOException {
		// TODO Auto-generated method stub
		//获取开始时间
		long start=System.currentTimeMillis();
		//一次读取一个字节数组
		//封装
		FileInputStream fis=new FileInputStream(string);
		FileOutputStream fos=new FileOutputStream(string2);
		
		
		byte[] bt=new byte[1024];
		int len;
		while((len=fis.read(bt))!=-1){
			fos.write(bt,0,len);
		}
		
		//释放资源
		fis.close();
		fos.close();
		//获取结束时间
		long end=System.currentTimeMillis();
		System.out.println("耗费时间:"+(end-start)+"毫秒!");

	}

	private static void Method1(String string, String string2) throws IOException {
		// TODO Auto-generated method stub
		//获取开始时间
		long start=System.currentTimeMillis();
		
		//一次读取一个字节
		//封装
		FileInputStream fis=new FileInputStream(string);
		FileOutputStream fos=new FileOutputStream(string2);
		while(fis.read()!=-1){
			fos.write(fis.read());
		}
		
		//释放资源
		fis.close();
		fos.close();
		//获取结束时间
		long end=System.currentTimeMillis();
		System.out.println("耗费时间:"+(end-start)+"毫秒!");
	}
}

结果:

耗费时间:144951毫秒!
耗费时间:302毫秒!
耗费时间:375毫秒!
耗费时间:70毫秒!

速度对比可知:

A:一次读取一个字节数组比一次读取一个快
B:缓冲流比文件流更快
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java后端指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值