1. 程式人生 > >Java 複製大檔案方式(nio2 FileChannel 拷貝檔案能力測試)

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 是最快的解決方案: