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中的@RestControllerAdvice注解使用解析
這篇文章主要介紹了Spring中的@RestControllerAdvice注解使用解析,@RestControllerAdvice?是?Spring?框架中一個用于統(tǒng)一處理控制器異常和返回結(jié)果的注解,它可以被用來定義全局異常處理程序和全局響應結(jié)果處理程序,需要的朋友可以參考下2024-01-01Oracle+Mybatis的foreach insert批量插入報錯的快速解決辦法
本文給大家介紹Oracle+Mybatis的foreach insert批量插入報錯的快速解決辦法,非常不錯,具有參考借鑒價值,感興趣的朋友參考下吧2016-08-08java連接postgresql數(shù)據(jù)庫代碼及maven配置方式
這篇文章主要介紹了java連接postgresql數(shù)據(jù)庫代碼及maven配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09解析Java的Spring框架的基本結(jié)構(gòu)
這篇文章主要介紹了Java的Spring框架的基本結(jié)構(gòu),作者從Spring的設計角度觸發(fā)解析其基礎的架構(gòu)內(nèi)容,需要的朋友可以參考下2016-03-03詳解Java中l(wèi)og4j.properties配置與加載應用
這篇文章主要介紹了 log4j.properties配置與加載應用的相關資料,需要的朋友可以參考下2018-02-02