欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)文件分割與合并

 更新時(shí)間:2020年05月19日 15:03:07   作者:linyaodonga  
這篇文章主要介紹了Java實(shí)現(xiàn)文件分割與合并,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java實(shí)現(xiàn)文件分割與合并的具體代碼,供大家參考,具體內(nèi)容如下

文件的操作

文件的分割

package com.xhh.util;

import java.io.*;

/**
 * 文件分割的方法
 * @param SrcFilePath 指定分割的文件路徑
 * @param SingleGoalFileSize 分割文件的個(gè)數(shù)
 * @param GoalFileDirectory 分割之后的路徑
 */
public class Split{
 public static void Split(String SrcFilePath,int SingleGoalFileSize,String GoalFileDirectory){
 //SingleGoalFileSize 單位:MB ,校驗(yàn)路徑和目錄
 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;//分割后的單個(gè)文件大小(單位字節(jié))

 int GoalFileNum = (int)(SrcFileSize/SingleFileSize); //獲取文件的大小
 GoalFileNum = SrcFileSize % SingleFileSize == 0 ? GoalFileNum : GoalFileNum + 1; //計(jì)算總的文件大小

 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++){
   //分割后的單個(gè)文件完整路徑名
   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,0,len);//從源文件讀取規(guī)定大小的字節(jié)數(shù)寫入到單個(gè)目標(biāo)文件中
    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();
  }
 }
}
}

運(yùn)行之后:

原文件

分割出來的文件

文件已經(jīng)成功的分割出來了

文件的合并

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;//將數(shù)據(jù)合并到目標(biāo)文件中
  BufferedOutputStream bos = null;//使用緩沖字節(jié)流寫入數(shù)據(jù)
  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]);//從分割后的文件讀取數(shù)據(jù)
    BufferedInputStream bis = new BufferedInputStream(fis);//使用緩沖字節(jié)流讀取數(shù)據(jù)
    while ((len = bis.read(bytes))!= -1)
     bos.write(bytes, 0, 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();
   }
  }
 }

運(yùn)行之后:

原文件

合并出來的文件

這樣文件已經(jīng)合并起來了,注意看路徑是不一樣的

測試

/**
 * 測試
 * @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 分割文件的個(gè)數(shù)  GoalFileDirectory 分割完成之后的路徑     
  String[] MergeFilePath = new String[5];//分割出來的文件個(gè)數(shù)
     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");//合并之后保存的路徑
 }

注意:

分割過的文件有時(shí)會出現(xiàn)文件損壞或丟失的情況,這時(shí)就必須由接收將這些文件合并才能回復(fù)原來的文件。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java使用continue語句的實(shí)例詳解

    Java使用continue語句的實(shí)例詳解

    這篇文章主要介紹了Java使用continue語句的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握使用方法,需要的朋友可以參考下
    2017-10-10
  • package打包一個(gè)springcloud項(xiàng)目的某個(gè)微服務(wù)報(bào)錯(cuò)問題

    package打包一個(gè)springcloud項(xiàng)目的某個(gè)微服務(wù)報(bào)錯(cuò)問題

    這篇文章主要介紹了package打包一個(gè)springcloud項(xiàng)目的某個(gè)微服務(wù)報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • java參數(shù)傳遞之值傳遞和引用傳遞

    java參數(shù)傳遞之值傳遞和引用傳遞

    這篇文章主要介紹了java參數(shù)傳遞之值傳遞和引用傳遞,引用了兩個(gè)代碼實(shí)例來講解,有感興趣的同學(xué)可以研究下
    2021-02-02
  • Java中雙冒號::的作用舉例詳解

    Java中雙冒號::的作用舉例詳解

    這篇文章主要給大家介紹了關(guān)于Java中雙冒號::作用的相關(guān)資料,雙冒號(::)運(yùn)算符在Java?8中被用作方法引用(method?reference),方法引用是與lambda表達(dá)式相關(guān)的一個(gè)重要特性,需要的朋友可以參考下
    2023-11-11
  • 智能 AI 代碼生成工具 Cursor 安裝和使用超詳細(xì)教程

    智能 AI 代碼生成工具 Cursor 安裝和使用超詳細(xì)教程

    Cursor.so 是一個(gè)集成了 GPT-4 的國內(nèi)直接可以訪問的,優(yōu)秀而強(qiáng)大的免費(fèi)代碼生成器,可以幫助你快速編寫、編輯和討論代碼,這篇文章主要介紹了智能 AI 代碼生成工具 Cursor 安裝和使用介紹,需要的朋友可以參考下
    2023-05-05
  • 值得收藏的SpringBoot 實(shí)用的小技巧

    值得收藏的SpringBoot 實(shí)用的小技巧

    最近分享的一些源碼、框架設(shè)計(jì)的東西。我發(fā)現(xiàn)大家熱情不是特別高,想想大多數(shù)應(yīng)該還是正兒八經(jīng)寫代碼的居多;這次就分享一點(diǎn)接地氣的: SpringBoot 使用中的一些小技巧 ,需要的朋友可以參考下
    2018-10-10
  • 基于SpringBoot+Avue實(shí)現(xiàn)短信通知功能

    基于SpringBoot+Avue實(shí)現(xiàn)短信通知功能

    Avue是基于vue和element-ui的快速開發(fā)框架 ,它的核心是數(shù)據(jù)驅(qū)動UI的思想,讓我們從繁瑣的crud開發(fā)中解脫出來,本文將給大家介紹一下使用SpringBoot+Avue實(shí)現(xiàn)短信通知功能,文中有詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-09-09
  • mybatis一級緩存和二級緩存的區(qū)別及說明

    mybatis一級緩存和二級緩存的區(qū)別及說明

    這篇文章主要介紹了mybatis一級緩存和二級緩存的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java IO流操作(PipeInputStream、SequenceInputStream、BufferedInputStream)

    Java IO流操作(PipeInputStream、SequenceInputStream、Buffered

    管道流主要用于線程間通信,分為管道輸入流(PipeInputStream)和管道輸出流(PipeOutputStream),本文介紹了如何通過管道流進(jìn)行數(shù)據(jù)發(fā)送和接收,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-10-10
  • 利用Lambda表達(dá)式創(chuàng)建新線程案例

    利用Lambda表達(dá)式創(chuàng)建新線程案例

    這篇文章主要介紹了利用Lambda表達(dá)式創(chuàng)建新線程案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08

最新評論