当前位置: 移动技术网 > IT编程>开发语言>Java > Java将文件分割为多个子文件再将子文件合并成原始文件的示例

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

刷qq等级,蹲点网,上海到海南旅游

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 
  { 
    /* 
    *将一个文件分割为多个文件,然后再组合成一个文件 
    * 找到文件,读入一个1m的buffer中,然后写入一个part文件中,循环此操作直到文件读取完毕 
    */ 
 
    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<integer> intset = new treeset<integer>(); 
    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<integer> sortedkeyset = new treeset<integer>(); 
    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();//关闭源文件流 
  } 
} 

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网