java高效實現(xiàn)大文件拷貝功能
在java中,F(xiàn)ileChannel類中有一些優(yōu)化方法可以提高傳輸?shù)男?,其中transferTo( )和 transferFrom( )方法允許將一個通道交叉連接到另一個通道,而不需要通過一個緩沖區(qū)來傳遞數(shù)據(jù)。只有FileChannel類有這兩個方法,因此 channel-to-channel 傳輸中通道之一必須是 FileChannel。不能在sock通道之間傳輸數(shù)據(jù),不過socket 通道實現(xiàn)WritableByteChannel 和 ReadableByteChannel 接口,因此文件的內(nèi)容可以用 transferTo( )方法傳輸給一個 socket 通道,或者也可以用 transferFrom( )方法將數(shù)據(jù)從一個 socket 通道直接讀取到一個文件中。
Channel-to-channel 傳輸是可以極其快速的,特別是在底層操作系統(tǒng)提供本地支持的時候。某些操作系統(tǒng)可以不必通過用戶空間傳遞數(shù)據(jù)而進行直接的數(shù)據(jù)傳輸。對于大量的數(shù)據(jù)傳輸,這會是一個巨大的幫助。
注意:如果要拷貝的文件大于4G,則不能直接用Channel-to-channel 的方法,替代的方法是使用ByteBuffer,先從原文件通道讀取到ByteBuffer,再將ByteBuffer寫到目標文件通道中。
下面為實現(xiàn)大文件快速拷貝的代碼:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class BigFileCopy {
/**
* 通過channel到channel直接傳輸
* @param source
* @param dest
* @throws IOException
*/
public static void copyByChannelToChannel(String source, String dest) throws IOException {
File source_tmp_file = new File(source);
if (!source_tmp_file.exists()) {
return ;
}
RandomAccessFile source_file = new RandomAccessFile(source_tmp_file, "r");
FileChannel source_channel = source_file.getChannel();
File dest_tmp_file = new File(dest);
if (!dest_tmp_file.isFile()) {
if (!dest_tmp_file.createNewFile()) {
source_channel.close();
source_file.close();
return;
}
}
RandomAccessFile dest_file = new RandomAccessFile(dest_tmp_file, "rw");
FileChannel dest_channel = dest_file.getChannel();
long left_size = source_channel.size();
long position = 0;
while (left_size > 0) {
long write_size = source_channel.transferTo(position, left_size, dest_channel);
position += write_size;
left_size -= write_size;
}
source_channel.close();
source_file.close();
dest_channel.close();
dest_file.close();
}
public static void main(String[] args) {
try {
long start_time = System.currentTimeMillis();
BigFileCopy.copyByChannelToChannel("source_file", "dest_file");
long end_time = System.currentTimeMillis();
System.out.println("copy time = " + (end_time - start_time));
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring Boot集成Druid數(shù)據(jù)庫連接池
這篇文章主要介紹了Spring Boot集成Druid數(shù)據(jù)庫連接池,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Java final static abstract關鍵字概述
這篇文章主要介紹了Java final static abstract關鍵字的相關資料,需要的朋友可以參考下2016-05-05

