java合并多個(gè)文件的兩種方法
在java多個(gè)線程下載文件或處理較大文件是可能會(huì)切分成多個(gè)文件,處理完成后需要合并成一個(gè)文件。
Java中合并子文件最容易想到的就是利用BufferedStream進(jìn)行讀寫。
利用BufferedStream合并多個(gè)文件
public static boolean mergeFiles(String[] fpaths, String resultPath) { if (fpaths == null || fpaths.length < 1 || TextUtils.isEmpty(resultPath)) { return false; } if (fpaths.length == 1) { return new File(fpaths[0]).renameTo(new File(resultPath)); } File[] files = new File[fpaths.length]; for (int i = 0; i < fpaths.length; i ++) { files[i] = new File(fpaths[i]); if (TextUtils.isEmpty(fpaths[i]) || !files[i].exists() || !files[i].isFile()) { return false; } } File resultFile = new File(resultPath); try { int bufSize = 1024; BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(resultFile)); byte[] buffer = new byte[bufSize]; for (int i = 0; i < fpaths.length; i ++) { BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(files[i])); int readcount; while ((readcount = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, readcount); } inputStream.close(); } outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } for (int i = 0; i < fpaths.length; i ++) { files[i].delete(); } return true; }
利用nio FileChannel合并多個(gè)文件
BufferedStream的合并操作是一個(gè)循環(huán)讀取子文件內(nèi)容然后復(fù)制寫入最終文件的過程,此過程會(huì)從文件系統(tǒng)中讀取數(shù)據(jù)到內(nèi)存中,之后再寫入文件系統(tǒng),比較低效。
一種更高效的合并方式是利用Java nio庫中FileChannel類的transferTo方法進(jìn)行合并。此方法可以利用很多操作系統(tǒng)直接從文件緩存?zhèn)鬏斪止?jié)的能力來優(yōu)化傳輸速度。
實(shí)現(xiàn)方法:
public static boolean mergeFiles(String[] fpaths, String resultPath) { if (fpaths == null || fpaths.length < 1 || TextUtils.isEmpty(resultPath)) { return false; } if (fpaths.length == 1) { return new File(fpaths[0]).renameTo(new File(resultPath)); } File[] files = new File[fpaths.length]; for (int i = 0; i < fpaths.length; i ++) { files[i] = new File(fpaths[i]); if (TextUtils.isEmpty(fpaths[i]) || !files[i].exists() || !files[i].isFile()) { return false; } } File resultFile = new File(resultPath); try { FileChannel resultFileChannel = new FileOutputStream(resultFile, true).getChannel(); for (int i = 0; i < fpaths.length; i ++) { FileChannel blk = new FileInputStream(files[i]).getChannel(); resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size()); blk.close(); } resultFileChannel.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } for (int i = 0; i < fpaths.length; i ++) { files[i].delete(); } return true; }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java實(shí)現(xiàn)文件切片和合并的代碼示例
- Java實(shí)現(xiàn)大文件的切割與合并操作示例
- java 流操作對(duì)文件的分割和合并的實(shí)例詳解
- Java實(shí)現(xiàn)文件分割和文件合并實(shí)例
- java 實(shí)現(xiàn)切割文件和合并文件的功能
- Java將文件分割為多個(gè)子文件再將子文件合并成原始文件的示例
- Java 使用IO流實(shí)現(xiàn)大文件的分割與合并實(shí)例詳解
- java實(shí)現(xiàn)合并2個(gè)文件中的內(nèi)容到新文件中
- java文件操作工具類實(shí)現(xiàn)復(fù)制文件和文件合并
- java實(shí)現(xiàn)大文件分割與合并的實(shí)例代碼
相關(guān)文章
SpringBoot 啟動(dòng)報(bào)錯(cuò)Unable to connect to 
這篇文章主要介紹了SpringBoot 啟動(dòng)報(bào)錯(cuò)Unable to connect to Redis server: 127.0.0.1/127.0.0.1:6379問題的解決方案,文中通過圖文結(jié)合的方式給大家講解的非常詳細(xì),對(duì)大家解決問題有一定的幫助,需要的朋友可以參考下2024-10-10Java中StringUtils工具類的一些用法實(shí)例
這篇文章主要介紹了Java中StringUtils工具類的一些用法實(shí)例,本文著重講解了isEmpty和isBlank方法的使用,另外也講解了trim、strip等方法的使用實(shí)例,需要的朋友可以參考下2015-06-06Java利用poi讀取Excel詳解實(shí)現(xiàn)
Apache POI 是用Java編寫的免費(fèi)開源的跨平臺(tái)的 Java API,Apache POI提供API給Java對(duì)Microsoft Office格式檔案讀和寫的功能。POI為“Poor Obfuscation Implementation”的首字母縮寫,意為簡潔版的模糊實(shí)現(xiàn)2022-07-07java連接MySQl數(shù)據(jù)庫實(shí)例代碼
這篇文章介紹了java連接MySQl數(shù)據(jù)庫實(shí)例代碼,有需要的朋友可以參考一下2013-10-10SpringBoot整合Mybatis Plus多數(shù)據(jù)源的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合Mybatis Plus多數(shù)據(jù)源的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11