java高效實(shí)現(xiàn)大文件拷貝功能
在java中,F(xiàn)ileChannel類中有一些優(yōu)化方法可以提高傳輸?shù)男剩渲衪ransferTo( )和 transferFrom( )方法允許將一個(gè)通道交叉連接到另一個(gè)通道,而不需要通過(guò)一個(gè)緩沖區(qū)來(lái)傳遞數(shù)據(jù)。只有FileChannel類有這兩個(gè)方法,因此 channel-to-channel 傳輸中通道之一必須是 FileChannel。不能在sock通道之間傳輸數(shù)據(jù),不過(guò)socket 通道實(shí)現(xiàn)WritableByteChannel 和 ReadableByteChannel 接口,因此文件的內(nèi)容可以用 transferTo( )方法傳輸給一個(gè) socket 通道,或者也可以用 transferFrom( )方法將數(shù)據(jù)從一個(gè) socket 通道直接讀取到一個(gè)文件中。
Channel-to-channel 傳輸是可以極其快速的,特別是在底層操作系統(tǒng)提供本地支持的時(shí)候。某些操作系統(tǒng)可以不必通過(guò)用戶空間傳遞數(shù)據(jù)而進(jìn)行直接的數(shù)據(jù)傳輸。對(duì)于大量的數(shù)據(jù)傳輸,這會(huì)是一個(gè)巨大的幫助。
注意:如果要拷貝的文件大于4G,則不能直接用Channel-to-channel 的方法,替代的方法是使用ByteBuffer,先從原文件通道讀取到ByteBuffer,再將ByteBuffer寫到目標(biāo)文件通道中。
下面為實(shí)現(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 { /** * 通過(guò)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(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的@RestControllerAdvice注解使用解析
這篇文章主要介紹了Spring中的@RestControllerAdvice注解使用解析,@RestControllerAdvice?是?Spring?框架中一個(gè)用于統(tǒng)一處理控制器異常和返回結(jié)果的注解,它可以被用來(lái)定義全局異常處理程序和全局響應(yīng)結(jié)果處理程序,需要的朋友可以參考下2024-01-01Oracle+Mybatis的foreach insert批量插入報(bào)錯(cuò)的快速解決辦法
本文給大家介紹Oracle+Mybatis的foreach insert批量插入報(bào)錯(cuò)的快速解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友參考下吧2016-08-08java連接postgresql數(shù)據(jù)庫(kù)代碼及maven配置方式
這篇文章主要介紹了java連接postgresql數(shù)據(jù)庫(kù)代碼及maven配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09解析Java的Spring框架的基本結(jié)構(gòu)
這篇文章主要介紹了Java的Spring框架的基本結(jié)構(gòu),作者從Spring的設(shè)計(jì)角度觸發(fā)解析其基礎(chǔ)的架構(gòu)內(nèi)容,需要的朋友可以參考下2016-03-03詳解Java中l(wèi)og4j.properties配置與加載應(yīng)用
這篇文章主要介紹了 log4j.properties配置與加載應(yīng)用的相關(guān)資料,需要的朋友可以參考下2018-02-02