一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2017-03-13 编辑:简简单单 来源:一聚教程网

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

 代码如下 复制代码
importjava.io.File;
importjava.io.InputStream;
importjava.io.FileInputStream;
importjava.io.OutputStream;
importjava.io.FileOutputStream;
importjava.util.Properties;
importjava.util.Iterator;
importjava.util.TreeSet;
importjava.util.Set;
  
publicclassTest
{
  publicstaticvoidmain(String[] args)throwsException
  {
    /*
    *将一个文件分割为多个文件,然后再组合成一个文件
    * 找到文件,读入一个1M的buffer中,然后写入一个Part文件中,循环此操作直到文件读取完毕
    */
  
    String sourceFilePath ="D:"+ File.separator +"Code"+ File.separator +"source"+ File.separator +"031316_【第13章:Java类集】_属性类:Properties.wmv";
    intpartFileLength =1024*1024;//指定分割的子文件大小为1M
    splitFile(sourceFilePath,partFileLength);//将文件分割
    combineFile("D:"+ File.separator +"Code"+ File.separator +"target");//将分割后的文件合并
  }
  
  publicstaticvoidcombineFile(String directoryPath)throwsException
  {
    Properties config =newProperties();
    InputStream ips =null;
    ips =newFileInputStream(newFile(directoryPath + File.separator +"config.properties"));
    config.load(ips);
  
    Set keySet = config.keySet();//需要将keySet转换为int型
      
      
    //将keySet迭代出来,转换成int类型的set,排序后存储进去
    Set intSet =newTreeSet();
    Iterator iterString = keySet.iterator();
    while(iterString.hasNext())
    {
      String tempKey = (String)iterString.next();
      if("name".equals(tempKey))
      {}
      else
      {
        inttempInt ;
        tempInt = Integer.parseInt(tempKey);
        intSet.add(tempInt);
      }
    }
      
    Set sortedKeySet =newTreeSet();
    sortedKeySet.addAll(intSet);
  
    OutputStream eachFileOutput =null;
    eachFileOutput =newFileOutputStream(newFile("D:"+ File.separator +"Code"+ File.separator + config.getProperty("name")));
  
    Iterator iter = sortedKeySet.iterator();
    while(iter.hasNext())
    {
      String key =newString(""+ 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 =newFileInputStream(newFile(filePath));
          
        byte[] buffer =newbyte[1024*1024*1];//缓冲区文件大小为1M
        intlen =0;
        while((len = eachFileInput.read(buffer,0,1024*1024*1)) != -1)
        {
          eachFileOutput.write(buffer,0,len);
        }
        eachFileInput.close();
      }
    }
  
    eachFileOutput.close();
  }
  
  publicstaticvoidsplitFile(String sourceFilePath,intpartFileLength)throwsException 
  {
    File sourceFile =null;
    File targetFile =null;
    InputStream ips =null;
    OutputStream ops =null;
    OutputStream configOps =null;//该文件流用于存储文件分割后的相关信息,包括分割后的每个子文件的编号和路径,以及未分割前文件名
    Properties partInfo =null;//properties用于存储文件分割的信息
    byte[] buffer =null;
    intpartNumber =1;
    sourceFile =newFile(sourceFilePath);//待分割文件
    ips =newFileInputStream(sourceFile);//找到读取源文件并获取输入流
    configOps =newFileOutputStream(newFile("D:"+ File.separator +"Code"//配置文件
      + File.separator +"target"+ File.separator +"config.properties"));
    buffer =newbyte[partFileLength];//开辟缓存空间
    inttempLength =0;
    partInfo =newProperties();//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 =newFile(targetFilePath);
      ops =newFileOutputStream(targetFile);//分割后文件
      ops.write(buffer,0,tempLength);//将信息写入碎片文件
  
      ops.close();//关闭碎片文件
    }
    partInfo.setProperty("name",sourceFile.getName());//存储源文件名
    partInfo.store(configOps,"ConfigFile");//将properties存储进实体文件中
    ips.close();//关闭源文件流
  }
}

热门栏目