1. 程式人生 > 程式設計 >Java實現檔案分割與合併

Java實現檔案分割與合併

本文例項為大家分享了Java實現檔案分割與合併的具體程式碼,供大家參考,具體內容如下

檔案的操作

檔案的分割

package com.xhh.util;

import java.io.*;

/**
 * 檔案分割的方法
 * @param SrcFilePath 指定分割的檔案路徑
 * @param SingleGoalFileSize 分割檔案的個數
 * @param GoalFileDirectory 分割之後的路徑
 */
public class Split{
 public static void Split(String SrcFilePath,int SingleGoalFileSize,String GoalFileDirectory){
 //SingleGoalFileSize 單位:MB ,校驗路徑和目錄
 if("".equals(SrcFilePath) || SrcFilePath == null || "".equals(GoalFileDirectory) || GoalFileDirectory == null){
  System.out.println("分割失敗!");
  return;
 }

 File SrcFile = new File(SrcFilePath); //新建檔案
 long SrcFileSize = SrcFile.length();//原始檔的大小
 long SingleFileSize = 1024 * 1024 * SingleGoalFileSize;//分割後的單個檔案大小(單位位元組)

 int GoalFileNum = (int)(SrcFileSize/SingleFileSize); //獲取檔案的大小
 GoalFileNum = SrcFileSize % SingleFileSize == 0 ? GoalFileNum : GoalFileNum + 1; //計算總的檔案大小

 int x1 = SrcFilePath.lastIndexOf("\\"); //獲取檔案路徑的分隔符位置
 int x2 = SrcFilePath.lastIndexOf("."); //獲取檔案的字尾位置

 String SrcFileName = SrcFilePath.substring(x1,x2); //擷取檔名

 FileInputStream fis = null;
 BufferedInputStream bis = null;
 byte bytes[] = new byte[1024 * 1024];//每次讀取檔案的大小
 int len = -1;

 try{ 
  fis = new FileInputStream(SrcFilePath); //新建輸入流物件
  bis = new BufferedInputStream(fis); 

  for(int i = 0; i < GoalFileNum; i++){
   //分割後的單個檔案完整路徑名
   String CompleteSingleGoalFilePath = GoalFileDirectory + File.separator + SrcFileName + "-" + i + SrcFilePath.substring(x2);
   FileOutputStream fos = new FileOutputStream(CompleteSingleGoalFilePath);
   BufferedOutputStream bos = new BufferedOutputStream(fos); //包裝
   int count = 0;
   while((len = bis.read(bytes))!=-1){
    bos.write(bytes,len);//從原始檔讀取規定大小的位元組數寫入到單個目標檔案中
    count += len;
    if(count >= SingleFileSize)
     break;
   }
   bos.flush();
   bos.close();
   fos.close();
  }
   System.out.println("分割成功!");
 }catch (FileNotFoundException e){
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 }finally {
  try{
   if(bis != null) {
    bis.close();
   }

   if(fis != null) {
    fis.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}
}

執行之後:

Java實現檔案分割與合併

原檔案

Java實現檔案分割與合併

分割出來的檔案

Java實現檔案分割與合併

檔案已經成功的分割出來了

檔案的合併

package com.xhh.util;

import java.io.*;

public class Merge { 
/**
 * @param SingleFilePath 
 * @param GoalFileDirectory 
 */
 public static void Merge(String SingleFilePath[],String GoalFileDirectory){
  if(GoalFileDirectory == null || "".equals(GoalFileDirectory)){
   System.out.println("合併失敗!");
   return;
  }

  int x1 = SingleFilePath[0].lastIndexOf("\\");
  int x2 = SingleFilePath[0].lastIndexOf(".");
  String GoalFileName = SingleFilePath[0].substring(x1,x2);

  //合併後的完整路徑名
  String CompleteGoalFilePath = GoalFileDirectory + File.separator + GoalFileName.substring(0,GoalFileName.lastIndexOf("-"))+ SingleFilePath[0].substring(x2);

  byte bytes[] = new byte[1024 * 1024];//每次讀取檔案的大小
  int len = -1;

  FileOutputStream fos = null;//將資料合併到目標檔案中
  BufferedOutputStream bos = null;//使用緩衝位元組流寫入資料
  try{
    fos = new FileOutputStream(CompleteGoalFilePath);
    bos = new BufferedOutputStream(fos);

   for(int i = 0; i < SingleFilePath.length; i++){
    if(SingleFilePath[i] == null || "".equals(SingleFilePath)){
     System.exit(0);
    }

    FileInputStream fis = new FileInputStream(SingleFilePath[i]);//從分割後的檔案讀取資料
    BufferedInputStream bis = new BufferedInputStream(fis);//使用緩衝位元組流讀取資料
    while ((len = bis.read(bytes))!= -1)
     bos.write(bytes,len);

    bis.close();
    fis.close();
   }
   System.out.println("合併成功!");
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    if (bos != null)
     bos.close();

    if(fos != null)
     fos.close();

   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

執行之後:

Java實現檔案分割與合併

原檔案

Java實現檔案分割與合併

合併出來的檔案

Java實現檔案分割與合併

這樣檔案已經合併起來了,注意看路徑是不一樣的

測試

/**
 * 測試
 * @param args
 */
public static void main(String[] args) {
//  Split("D:\\qycache\\download\\dpx\\dpx3.qvs",10,"D:\\qycache\\download\\hhhh");//
//     Split(SrcFilePath,SingleGoalFileSize,GoalFileDirectory);
//     SrcFilePath 指定分割的檔案路徑  SingleGoalFileSize 分割檔案的個數  GoalFileDirectory 分割完成之後的路徑     
  String[] MergeFilePath = new String[5];//分割出來的檔案個數
     for(int i = 0; i < MergeFilePath.length; i++)
       MergeFilePath[i] = new String("D:\\qycache\\download\\hhhh\\dpx3-" + i + ".qsv");//想要合併的檔案路徑
       Merge(MergeFilePath,"D:\\qycache\\download\\jjjj");//合併之後儲存的路徑
 }

注意:

分割過的檔案有時會出現檔案損壞或丟失的情況,這時就必須由接收將這些檔案合併才能回覆原來的檔案。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。