栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java将文件分割为多个子文件再将子文件合并成原始文件的示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java将文件分割为多个子文件再将子文件合并成原始文件的示例

Java将文件分割为多个子文件再将子文件合并成原始文件的示例,废话不多说,代码如下:

import java.io.File; 
import java.io.InputStream; 
import java.io.FileInputStream; 
import java.io.OutputStream; 
import java.io.FileOutputStream; 
import java.util.Properties; 
import java.util.Iterator; 
import java.util.TreeSet; 
import java.util.Set; 
 
public class Test 
{ 
  public static void main(String[] args) throws Exception 
  { 
     
 
    String sourceFilePath = "D:" + File.separator + "Code" + File.separator + "source" + File.separator + "031316_【第13章:Java类集】_属性类:Properties.wmv"; 
    int partFileLength = 1024*1024;//指定分割的子文件大小为1M 
    splitFile(sourceFilePath,partFileLength);//将文件分割 
    combineFile("D:" + File.separator + "Code" + File.separator + "target");//将分割后的文件合并 
  } 
 
  public static void combineFile(String directoryPath) throws Exception 
  { 
    Properties config = new Properties(); 
    InputStream ips = null; 
    ips = new FileInputStream(new File(directoryPath + File.separator + "config.properties")); 
    config.load(ips); 
 
    Set keySet = config.keySet();//需要将keySet转换为int型 
     
     
    //将keySet迭代出来,转换成int类型的set,排序后存储进去 
    Set intSet = new TreeSet(); 
    Iterator iterString = keySet.iterator(); 
    while(iterString.hasNext()) 
    { 
      String tempKey = (String)iterString.next(); 
      if("name".equals(tempKey)) 
      {} 
      else 
      { 
 int tempInt ; 
 tempInt = Integer.parseInt(tempKey); 
 intSet.add(tempInt); 
      } 
    } 
     
    Set sortedKeySet = new TreeSet(); 
    sortedKeySet.addAll(intSet); 
 
    OutputStream eachFileOutput = null; 
    eachFileOutput = new FileOutputStream(new File("D:" + File.separator + "Code" + File.separator + config.getProperty("name"))); 
 
    Iterator iter = sortedKeySet.iterator(); 
    while(iter.hasNext()) 
    { 
      String key = new String("" + iter.next()); 
      if(key.equals("name")) 
      {} 
      else 
      { 
 //System.out.println("debug---"); 
 String fileNumber = null; 
 String filePath = null; 
 fileNumber = key; 
 filePath = config.getProperty(fileNumber); 
 
 //循环读取文件 --> 依次写入 
 
 InputStream eachFileInput = null; 
  
 eachFileInput = new FileInputStream(new File(filePath)); 
  
 byte[] buffer = new byte[1024*1024*1];//缓冲区文件大小为1M 
 int len = 0; 
 while((len = eachFileInput.read(buffer,0,1024*1024*1)) != -1) 
 { 
   eachFileOutput.write(buffer,0,len); 
 } 
 eachFileInput.close(); 
      } 
    } 
 
    eachFileOutput.close(); 
  } 
 
  public static void splitFile(String sourceFilePath,int partFileLength) throws Exception  
  { 
    File sourceFile = null; 
    File targetFile = null; 
    InputStream ips = null; 
    OutputStream ops = null; 
    OutputStream configOps = null;//该文件流用于存储文件分割后的相关信息,包括分割后的每个子文件的编号和路径,以及未分割前文件名 
    Properties partInfo = null;//properties用于存储文件分割的信息 
    byte[] buffer = null; 
    int partNumber = 1; 
    sourceFile = new File(sourceFilePath);//待分割文件 
    ips = new FileInputStream(sourceFile);//找到读取源文件并获取输入流 
    configOps = new FileOutputStream(new File("D:" + File.separator + "Code" //配置文件 
      + File.separator + "target" + File.separator + "config.properties")); 
    buffer = new byte[partFileLength];//开辟缓存空间 
    int tempLength = 0; 
    partInfo = new Properties();//key:1开始自动编号 value:文件路径 
 
    while((tempLength = ips.read(buffer,0,partFileLength)) != -1)  
    { 
      String targetFilePath = "D:" + File.separator + "Code"  
 + File.separator + "target" + File.separator + "part_" + (partNumber);//分割后的文件路径+文件名 
      partInfo.setProperty((partNumber++)+"",targetFilePath);//将相关信息存储进properties 
      targetFile = new File(targetFilePath); 
      ops = new FileOutputStream(targetFile);//分割后文件 
      ops.write(buffer,0,tempLength);//将信息写入碎片文件 
 
      ops.close();//关闭碎片文件 
    } 
    partInfo.setProperty("name",sourceFile.getName());//存储源文件名 
    partInfo.store(configOps,"ConfigFile");//将properties存储进实体文件中 
    ips.close();//关闭源文件流 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/147881.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号