文件复制性能比较Java

该博客通过Java代码展示了四种不同的文件复制方法,包括使用普通字节流、缓冲字节流分别进行单个字节和字节数组的复制。通过对不同方法的运行时间进行测量,揭示了缓冲流在文件操作中的效率优势。作者指出,利用Java 7的try-with-resources特性,可以自动关闭流,避免资源泄露。
摘要由CSDN通过智能技术生成

我们知道Java中使用IO流来复制文件的方式有很多种,大体上分为采用字节流和缓冲字节流来进行读写文件。

直接上代码:

import java.io.*;
public class Text_文件复制性能分析 {
  //定义文件复制时源文件的抽象路径,若地址有问题则会报FileNotFindException
  public static final String address1 = "D:\\www\\TIM3.2.0.21856.exe";
  public static final String address2 = "D:\\www\\baidu\\";
  public static void main(String[] args) throws Exception {
  copy1();  //字节流(一次读取一个字节)
  copy2();  //字节流(一次读取一个字节数组)
  copy3();   //缓冲字节流(一次读取一个字节)
  copy4();   //缓冲流(一次读取一个字节数组)
}
//普通流进行单个字节进行复制
  public static void copy1() throws Exception {
	  long start = System.currentTimeMillis();
	  try(
			FileInputStream file = new FileInputStream(address1);
			FileOutputStream file2 = new FileOutputStream(address2+"1.exe");			  
			  ){
		  int len;
		  while((len = file.read()) != -1) {
			  file2.write(len);
		  }  
	  }catch(Exception e) {
		  System.out.println(e);
	  }
	  long end = System.currentTimeMillis();
	  System.out.println("普通流单个字节进行复制花费时间为:"+(end-start)/1000+"秒");
  }
//普通流按照字节数组的形式进行复制
  public static void copy2() throws Exception {
	  long start = System.currentTimeMillis();
	  try(
			FileInputStream file = new FileInputStream(address1);
			FileOutputStream file2 = new FileOutputStream(address2+"2.exe");			  
			  ){
		  byte[] by = new byte[1024];
		  int len;
		  while((len = file.read(by)) != -1) {
			  file2.write(by,0,len);
		  }  
	  }catch(Exception e) {
		  System.out.println(e);
	  }
	  long end = System.currentTimeMillis();
	  System.out.println("普通流字节数组进行复制花费时间为:"+(end-start)/1000+"秒");
  }
  //缓冲流单字节进行复制
  public static void copy3() throws Exception {
	  long start = System.currentTimeMillis();
	  try(
			FileInputStream file = new FileInputStream(address1);
			FileOutputStream file2 = new FileOutputStream(address2+"3.exe");
			BufferedInputStream bis = new BufferedInputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(file2);
			 //简写
			 //InputStream bis = new BufferedInputStream(new FileInputStream(address1));
		    //OutputStream bos = new BufferedOutputStream(new FileOutputStream(adderss2+"3.exe");
			  ){
		  int len;
		  while((len = bis.read()) != -1) {
			  bos.write(len);
		  }  
	  }catch(Exception e) {
		  System.out.println(e);
	  }
	  long end = System.currentTimeMillis();
	  System.out.println("缓冲流单个字节进行复制花费时间为:"+(end-start)/1000.0+"秒");
  }
  //缓冲流字节数组进行复制
  public static void copy4() throws Exception {
	  long start = System.currentTimeMillis();
	  try(
			FileInputStream file = new FileInputStream(address1);
			FileOutputStream file2 = new FileOutputStream(address2+"4.exe");
		    BufferedInputStream bis = new BufferedInputStream(file);
		    BufferedOutputStream bos = new BufferedOutputStream(file2);
		    //简写为:
		    //InputStream bis = new BufferedInputStream(new FileInputStream(address1));
		    //OutputStream bos = new BufferedOutputStream(new FileOutputStream(adderss2+"4.exe");
			
			  ){
		  byte[] by = new byte[1024];
		  int len;
		  while((len = bis.read(by)) != -1) {
			  bos.write(by,0,len);
		  }  
	  }catch(Exception e) {
		  System.out.println(e);
	  }
	  long end = System.currentTimeMillis();
	  System.out.println("缓冲流字节数组进行复制花费时间为:"+(end-start)/1000+"秒");
  }  
}

//输出结果:
//输出结果和方法执行的顺序没有关系,因为我的计时器是定义在方法中的,所以和它的先后顺序无关!
// 734秒
// 1秒
// 1秒
// 0秒

代码比较简单,但是有个问题要说明一下

为什么我的IO流在使用结束后没有调用close方法释放资源?

答:可以发现我在创建IO流对象的时候是直接放在了try()里。这是jdk的新特性。但是try()里只能存放IO流对象,它在结束后会自动释放资源。因此我们要知道什么是IO流,其实IO流都会实现一个接口(Closeable)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值