java工具类之大文件分片(切割)与合并
生活随笔
收集整理的這篇文章主要介紹了
java工具类之大文件分片(切割)与合并
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前在潭州教育教學網站上看了一個視頻關于java大文件的分片與合并
自己在練習的時候遇到一些坑,調試了好長時間
代碼如下:
首先配置一個專門放參數的類
SplitFileParam
public class SplitFileParam {public static String file="C:\\Users\\pc\\Desktop\\photo/1.jpg"; //文件的路徑public static String outfile="C:\\Users\\pc\\Desktop\\photo/out.jpg"; //文件的路徑public static int count=10; //將文件切割成多少份 }SplitFile
package SplitFileUtil;import jdk.nashorn.internal.runtime.logging.Logger;import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile;public class SplitFile {public static void main(String[] args) {getSplitFile();merge(SplitFileParam.outfile,SplitFileParam.file,10);}/*** 文件分割方法*/public static void getSplitFile() {String file = SplitFileParam.file; //文件的路徑int count = SplitFileParam.count; //文件分割的份數RandomAccessFile raf = null;try {//獲取目標文件 預分配文件所占的空間 在磁盤中創建一個指定大小的文件 r 是只讀raf = new RandomAccessFile(new File(file), "r");long length = raf.length();//文件的總長度long maxSize = length / count;//文件切片后的長度long offSet = 0L;//初始化偏移量for (int i = 0; i < count - 1; i++) { //最后一片單獨處理long begin = offSet;long end = (i + 1) * maxSize; // offSet = writeFile(file, begin, end, i);offSet = getWrite(file, i, begin, end);}if (length - offSet > 0) {getWrite(file, count-1, offSet, length);}} catch (FileNotFoundException e) {System.out.println("沒有找到文件");e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {raf.close();} catch (IOException e) {e.printStackTrace();}}}/*** 指定文件每一份的邊界,寫入不同文件中* @param file 源文件* @param index 源文件的順序標識* @param begin 開始指針的位置* @param end 結束指針的位置* @return long*/public static long getWrite(String file,int index,long begin,long end){String a=file.split(".jpg")[0];long endPointer = 0L;try {//申明文件切割后的文件磁盤RandomAccessFile in = new RandomAccessFile(new File(file), "r");//定義一個可讀,可寫的文件并且后綴名為.tmp的二進制文件RandomAccessFile out = new RandomAccessFile(new File(a + "_" + index + ".tmp"), "rw");//申明具體每一文件的字節數組byte[] b = new byte[1024];int n = 0;//從指定位置讀取文件字節流in.seek(begin);//判斷文件流讀取的邊界while(in.getFilePointer() <= end && (n = in.read(b)) != -1){//從指定每一份文件的范圍,寫入不同的文件out.write(b, 0, n);}//定義當前讀取文件的指針endPointer = in.getFilePointer();//關閉輸入流in.close();//關閉輸出流out.close();} catch (Exception e) {e.printStackTrace();}return endPointer;}/*** 文件合并* @param file 指定合并文件* @param tempFile 分割前的文件名* @param tempCount 文件個數*/public static void merge(String file,String tempFile,int tempCount) {String a=tempFile.split(".jpg")[0];RandomAccessFile raf = null;try {//申明隨機讀取文件RandomAccessFileraf = new RandomAccessFile(new File(file), "rw");//開始合并文件,對應切片的二進制文件for (int i = 0; i < tempCount; i++) {//讀取切片文件RandomAccessFile reader = new RandomAccessFile(new File(a + "_" + i + ".tmp"), "r");byte[] b = new byte[1024];int n = 0;//先讀后寫while ((n = reader.read(b)) != -1) {//讀raf.write(b, 0, n);//寫}}} catch (Exception e) {e.printStackTrace();} finally {try {raf.close();} catch (IOException e) {e.printStackTrace();}}}}上面是運行后完整的代碼。
下面就說說我遇到坑,也怪我不細心
在分割文件的方法中,將兩個條件弄反了,導致丟失一般的數據
上面是正確的截圖
將兩個條件反過來之后
結果是:
原因就是:
while(in.getFilePointer() <= end && (n = in.read(b)) != -1){while( (n = in.read(b)) != -1&&in.getFilePointer() <= end){上面是數據的指針是從零開始的
而下面的數據指針是從in讀過之后的位置開始的,就是上面與下面的數據指針位置相差一個之前定義的byte數組長度個長度,這就是我測試是,數據丟失的原因
--------------------- 本文來自 YG青松 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/qq_38553333/article/details/80435392?utm_source=copy
總結
以上是生活随笔為你收集整理的java工具类之大文件分片(切割)与合并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: appinventor2 MySQL,写
- 下一篇: 智明OA漏洞学习——EmailDownl