Java 复制大文件方式(nio2 FileChannel 拷贝文件能力测试)

Java 拷贝文件的方式很多,除了 FileChannel 提供的方法外,还包括使用 Files.copy() 或使用字节数组的缓冲/非缓冲流。那个才是最好的选择呢?这个问题很难回答,因为答案基于很多因素。本文将目光集中到一个因素,那就是速度,因为拷贝任务 越快将会提高效率,在有些情况下,这是成功的关键。因此,本文将使用一个应用程序来比较下面这些拷贝方式的具体时间:

  • FileChannel 和非直接模式的 ByteBuffer
  • FileChannel 和直接模式的 ByteBuffer
  • FileChannel.transferTo()
  • FileChannel.transferFrom()
  • FileChannel.map()
  • 使用字节数组和缓冲流
  • 使用字节数组和非缓冲流
  • File.copy()(Path 到 Path,InputStream 到 Path 和 Path 到 OutputStream)

应用程序基于下面的条件:

  • 拷贝文件类型 MP4 视频(文件名为 Rafa Best Shots.mp4,所在目录为 C:\rafaelnadal\tournaments\2009\videos)
  • 文件大小:58.3MB
  • 测试的缓冲区大小:4KB, 16KB, 32KB, 64KB, 128KB, 256KB, and 1024KB
  • 机器配置:Mobile AMD Sempron Processor 3400 + 1.80 GHz, 1.00GB RAM, 32-bit
    OS, Windows 7 Ultimate
  • 测量类型:使用 System.nanoTime() 方法
  • 连续运行三次后再获取时间;前三次运行将会被忽略。开始运行的时间总会比后面运行的时间要长一些。

下面将列出完整的应用程序:

package com.my.download;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;

public class FileCopy {
	
	
	 private final static Path copy_from = Paths.get("C:/rafaelnadal/tournaments/2009/videos/Rafa Best Shots.mp4");
	 private final static Path copy_to = Paths.get("C:/Rafa Best Shots.mp4");
	 private static long startTime, elapsedTime;
	 private static int bufferSizeKB = 4;//also tested for 16, 32, 64, 128, 256 and 1024
	 private static int bufferSize = bufferSizeKB * 1024;
	

	public static void main(String[] args) throws Exception {
		
		transferfrom();
		
		transferTo();
		
		nonDirectBuffer();
		
		directBuffer();
		
		mapperedBuffer();
		
		ioBufferedStream();
		
		ioUnBufferedStream();
		
		copyPath2Path();
		
		copyInputStream2Path();
		
		copyPath2OutputStream();
		
		//randomReadFile();
		
	}
	
	
	
	public static void transferfrom() {
		
		 try (FileChannel fileChannel_from = (FileChannel.open(copy_from,   
		                      EnumSet.of(StandardOpenOption.READ)));
		      FileChannel fileChannel_to = (FileChannel.open(copy_to,  
		                      EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
		  
		      startTime = System.nanoTime();
		      fileChannel_to.transferFrom(fileChannel_from, 0L, (int) fileChannel_from.size());
		      elapsedTime = System.nanoTime() - startTime;
		      System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		 }catch (IOException ex) {
		   System.err.println(ex);
		 }
		 deleteCopied(copy_to);    
	}
	
	public static void transferTo() throws Exception{
		
		try (FileChannel fileChannel_from = (FileChannel.open(copy_from,  
		                      EnumSet.of(StandardOpenOption.READ)));
		      FileChannel fileChannel_to = (FileChannel.open(copy_to,  
		                      EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
		  
		      startTime = System.nanoTime();
		  
		      fileChannel_from.transferTo(0L, fileChannel_from.size(), fileChannel_to);
		  
		      elapsedTime = System.nanoTime() - startTime;
		      System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		}catch (IOException ex) {
		   System.err.println(ex);
		}
		deleteCopied(copy_to);
		
	}
	
	public static void nonDirectBuffer(){
		
		 try (
			    FileChannel fileChannel_from = FileChannel.open(copy_from,  
	                     EnumSet.of(StandardOpenOption.READ));
				FileChannel fileChannel_to = FileChannel.open(copy_to,  
	                     EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE));){ 
		  
		      startTime = System.nanoTime();
		      ByteBuffer bytebuffer = ByteBuffer.allocate(bufferSize);
		      int bytesCount;
		      while ((bytesCount = fileChannel_from.read(bytebuffer)) > 0) {
		       bytebuffer.flip();
		       fileChannel_to.write(bytebuffer);
		       bytebuffer.clear();
		      }
		        
		      elapsedTime = System.nanoTime() - startTime;
		      System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		 }catch (IOException ex) {
			  System.err.println(ex);
		 }
		 deleteCopied(copy_to);
	}
	
	public static void directBuffer(){
		 try (
			 FileChannel fileChannel_to = FileChannel.open(copy_to,  
	                 EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE));
			 FileChannel fileChannel_from = (FileChannel.open(copy_from,  
		    	                 EnumSet.of(StandardOpenOption.READ)));) {

		 startTime = System.nanoTime();
		 ByteBuffer bytebuffer = ByteBuffer.allocateDirect(bufferSize);
		 int bytesCount;
		 while ((bytesCount = fileChannel_from.read(bytebuffer)) > 0) {
			  bytebuffer.flip();
			  fileChannel_to.write(bytebuffer);
			  bytebuffer.clear();
		 }
		   
		 elapsedTime = System.nanoTime() - startTime;
		 System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		}catch (IOException ex) {
		System.err.println(ex);
		}
		
		deleteCopied(copy_to);
	}
	
	
	public static void mapperedBuffer() throws Exception{
		
		
	    try (FileChannel fileChannel_from = (FileChannel.open(copy_from,  
	                 EnumSet.of(StandardOpenOption.READ)));
				 FileChannel fileChannel_to = (FileChannel.open(copy_to,  
				                 EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
				                   
				startTime = System.nanoTime();
				int i=0;
				long size=fileChannel_from.size()/30;
				ByteBuffer rr,ww=null;
				while((i<fileChannel_from.size()) && ((fileChannel_from.size()-i))>size){
					rr=fileChannel_from.map(MapMode.READ_ONLY, i, size);
					ww=fileChannel_to.map(MapMode.READ_WRITE, i, size);
					ww.put(rr);
					rr.clear();
					ww.clear();
					i+=size;
				}
				
				rr=fileChannel_from.map(MapMode.READ_ONLY, i, fileChannel_from.size()-i);
				ww=fileChannel_to.map(MapMode.READ_WRITE, i, fileChannel_from.size()-i);
				ww.put(rr);
				rr.clear();
				ww.clear();
				elapsedTime = System.nanoTime() - startTime;
				System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
				
	    }catch (IOException ex) {
				System.err.println(ex);
		}
				     
		deleteCopied(copy_to); 
		
	}
	
	
	public static void ioBufferedStream(){
		
		File inFileStr = copy_from.toFile();
		File outFileStr = copy_to.toFile();
		try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFileStr));
		     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFileStr))) {
		     startTime = System.nanoTime();
		     byte[] byteArray = new byte[bufferSize];
		     int bytesCount;
		     while ((bytesCount = in.read(byteArray)) != -1) {
		             out.write(byteArray,0, bytesCount);
		     }
		     elapsedTime = System.nanoTime() - startTime;
		     System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		}catch (IOException ex) {
		  System.err.println(ex);
		}
		deleteCopied(copy_to);
	}
	
	
	public static void ioUnBufferedStream(){
		
		File inFileStr = copy_from.toFile();
		File outFileStr = copy_to.toFile();
		try (FileInputStream in = new FileInputStream(inFileStr);
			     FileOutputStream out = new FileOutputStream(outFileStr)) {
			     startTime = System.nanoTime();
			     byte[] byteArray = new byte[bufferSize];
			     int bytesCount;
			     while ((bytesCount = in.read(byteArray)) != -1) {
			             out.write(byteArray,0, bytesCount);
			     }
			  
			     elapsedTime = System.nanoTime() - startTime;
			     System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
			}catch (IOException ex) {
			  System.err.println(ex);
			}
			deleteCopied(copy_to);
	}
	
	
	public static void copyPath2Path(){
		try {
		    startTime = System.nanoTime();
		  
		    Files.copy(copy_from, copy_to, NOFOLLOW_LINKS);
		  
		    elapsedTime = System.nanoTime() - startTime;
		    System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		}catch (IOException e) {
		  System.err.println(e);
		}
		deleteCopied(copy_to);
	}
	
	
	public static void copyInputStream2Path(){
		try (InputStream is = new FileInputStream(copy_from.toFile())) {
			  
		    startTime = System.nanoTime();
		    Files.copy(is, copy_to);
		    elapsedTime = System.nanoTime() - startTime;
		    System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		}catch (IOException e) {
		  System.err.println(e);
		}
		deleteCopied(copy_to);
	}
	
	
	public static void copyPath2OutputStream(){
		try (OutputStream os = new FileOutputStream(copy_to.toFile())) {
		     startTime = System.nanoTime();
		     Files.copy(copy_from, os);
		     elapsedTime = System.nanoTime() - startTime;
		     System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
		 }catch (IOException e) {
		   System.err.println(e);
		 }
		
	}
	
	
	public static void randomReadFile(){
		
		try(	
			RandomAccessFile read = new RandomAccessFile("C:\\Users\\asus\\Desktop\\cn_windows_7_ultimate_with_sp1_x86_dvd_618763.iso","r");
			RandomAccessFile writer = new RandomAccessFile("C:\\Users\\asus\\Desktop\\dwTest\\cn_windows_7_ultimate_with_sp1_x86_dvd_618763.iso","rw");){
			startTime = System.nanoTime();
			byte[] b = new byte[200*1024*1024];
			while(read.read(b)!=-1){
				writer.write(b);
			}
			elapsedTime = System.nanoTime() - startTime;
			System.out.println("Elapsed Time is " + (elapsedTime / 1000000000.0) + " seconds");
			
		}catch(Exception e){
			System.err.println(e);
		}
		deleteCopied(copy_to);
	}
	
	
	public static void deleteCopied(Path path){
		  try {
		      Files.deleteIfExists(path);
		  }catch (IOException ex) {
		    System.err.println(ex);
		  }
		          
	}
}


输出结果排序比较复杂,其中包含了很多数据。下面我将主要的对比用图形的方式展示出来。图形中 Y 坐标表示消耗的时间(单位:秒),X 坐标表示缓冲的大小(或运行次数,跳过了前三次运行)。

FileChannel 和非直接模式 Buffer vs. FileChannel 和直接模式 Buffer

从下图看来,如果缓存小于 256KB,那么非直接模式的 Buffer 快一点,而缓存大于 256KB 后,直接模式的 Buffer 快一点:

FileChannel.transferTo() vs. FileChannel.transferFrom() vs. FileChannel.map()

从下图看来,FileChannel.transferTo() 和 FileChannel.transferFrom 运行七次的速度都差不多,而 FileChannel.map 的速度就要差很多:

三种 Files.copy() 方法

从下图看来,最快的是 Path 到 Path,其次是 Path 到 OutputStream,最慢的是 InputStream 到 Path:

FileChannel 和非直接模式 Buffer vs. FileChannel.transferTo() vs. Path 到 Path

最后,我们将前面最快的三种方式综合起来比较。从比较的结果来看,似乎 Path 到 Path 是最快的解决方案:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,您可以使用NIO(New I/O)来拷贝文件NIO提供了更高效的I/O操作方式,特别是在处理大文件时。以下是一个使用NIO拷贝文件的示例代码: ```java import java.io.IOException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; public class FileCopyExample { public static void main(String[] args) { String sourceFile = "path/to/source/file.txt"; // 源文件路径 String destinationFile = "path/to/destination/file.txt"; // 目标文件路径 try { // 创建输入流和输出流 FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(destinationFile); // 获取输入流和输出流的通道 FileChannel sourceChannel = fis.getChannel(); FileChannel destinationChannel = fos.getChannel(); // 使用 transferTo() 方法拷贝文件 destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); // 关闭通道和流 sourceChannel.close(); destinationChannel.close(); fis.close(); fos.close(); System.out.println("文件拷贝完成"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述示例中,您需要将 `sourceFile` 和 `destinationFile` 的值替换为实际的源文件路径和目标文件路径。代码将打开源文件和目标文件的输入流和输出流,并获取它们的通道。然后,通过调用 `transferFrom()` 方法来拷贝文件数据。最后,关闭通道和流。 请注意,以上代码只是一个简单的示例,没有处理异常情况和错误处理。在实际的应用中,您可能需要添加适当的异常处理和错误检查。 另外,还有其他一些方法可以使用NIO拷贝文件,例如使用 `transferTo()` 方法、使用 `read()` 和 `write()` 方法逐个字节拷贝等。您可以根据自己的需求选择适合的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值