用Java实现大文件的分割与合并

xiaoxiao2021-02-28  82

我们平时在发送文件或上传文件的时候会遇到文件大小的限制。如果文件过大,那么我们就可以将其分割,由接收方来将这些文件合并。其本质不过就是文件的读写操作。

文件的分割

public static void Split(String SrcFilePath,int SingleGoalFileSize,String GoalFileDirectory){//SingleGoalFileSize单位:M 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,0,len);//从源文件读取规定大小的字节数写入到单个目标文件中 count += len; if(count >= SingleFileSize) break; } bos.flush(); bos.close(); fos.close(); } }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(); } } }

文件的合并

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, 0, len); bis.close(); fis.close(); } } 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(); } } }

测试主函数

public static void main(String args[]){ //Split("E:\\1\\video.mp4", 20, "E:\\2"); String[] MergeFilePath = new String[14]; for(int i = 0; i < MergeFilePath.length; i++) MergeFilePath[i] = new String("E:\\2\\video-" + i + ".mp4"); Merge(MergeFilePath,"E:\\1"); }

注意

分割过的文件有时会出现文件损坏或信息丢失的情况(比如视频分割),这时就必须由接收方将这些文件合并才能恢复成原来的文件。其实大文件的分割与合并只是一种手段而已,在这过程中不必计较分割后的文件是否能够打开,我们真正关心和最终想要的是对方因发送容量限制而不得不分割的大文件。
转载请注明原文地址: https://www.6miu.com/read-74168.html

最新回复(0)