文件复制性能比较Java

it2025-08-09  9

我们知道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)

最新回复(0)