文件流的合并与拆分

import java.io.*;
import java.util.*;

/**
 * 文件流合并
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author mhj
 * @version 1.0
 */
public class CoalitionFiles {
  private Vector vt;
  private String outfile = "";
  private FileInputStream in;
  private FileOutputStream out;
  private String _temp_info = "";
  private int nNumber;
  private byte[] buffer;
  private static final int BUFFERLENGTH = 2048; //缓冲区大小

  public CoalitionFiles() {
    this.vt = new Vector();
  }

  /**
   * 设置要合并的文件名,要求绝对地址
   * @param v Vector
   */
  public void setInputFiles(Vector v) {
    this.vt = v;
  }

  public void setInputFiles(String infile) {
    this.vt.addElement(infile);
  }

  public void setOutfile(String outfile) {
    this.outfile = outfile;
  }

  public void coalition() {
    if (isAllowToCoalition()) {
      try {
        //创建文件输出流对象
        out = new FileOutputStream(this.outfile);

        //写入合并文件文件信息
        byte[] bytes = (this._temp_info + "/n").getBytes();
        out.write(bytes);

        for (int i = 0; i < this.vt.size(); i++) {
          //创建文件输入流对象
          in = new FileInputStream(this.vt.elementAt(i).toString());
          //向压缩文件中输出数据
          buffer = new byte[this.BUFFERLENGTH];
          while ( (nNumber = in.read(buffer)) != -1) {
            out.write(buffer, 0, nNumber);
          }
        }

        //关闭创建的流对象
        this.vt.clear();
        out.close();
        in.close();
      }
      catch (IOException e) {
        System.err.println("IO错误!");
      }
    }
  }

  private boolean isAllowToCoalition() {
    boolean b = true;
    if (this.vt == null) {
      System.err.println("没有设置要合并的文件名!");
      b = false;
    }

    if (this.outfile.equals("")) {
      System.err.println("没有设置要合并后的新文件名!");
      b = false;
    }

    long startpos = 0;
    long endpos = 0;
    for (int i = 0; i < this.vt.size(); i++) {
      File f = new File(this.vt.elementAt(i).toString());
      if (!f.exists()) {
        System.err.println("找不到要合并的文件:" + f.getPath());
        b = false;
      }
      else {
        endpos = startpos + f.length();
        this._temp_info += f.getName() + ":" + startpos + ":" + endpos + "/";
        startpos += f.length();
      }
    }

    return b;
  }


//  public static void main(String[] args) {
//    CoalitionFiles coalitionFiles1 = new CoalitionFiles();
//    Vector a = new Vector();
//    a.addElement("d://test//1.txt");
//    a.addElement("d://test//2.txt");
//    a.addElement("d://test//1.rar");
//    a.addElement("d://test//2.rar");
//    a.addElement("d://test//3.rar");
//    a.addElement("d://test//4.rar");
//    a.addElement("d://test//5.rar");
//    a.addElement("d://test//6.rar");
//    a.addElement("d://test//7.rar");
//    a.addElement("d://test//8.iso");
//    a.addElement("d://test//9.iso");
//    a.addElement("d://test//10.iso");
//    a.addElement("d://test//11.iso");
//    coalitionFiles1.setInputFiles(a);
//    coalitionFiles1.setOutfile("d://test//AA");
//    coalitionFiles1.coalition();
//  }
}


 

import java.io.*;

/**
 * 文件流拆分
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author mhj
 * @version 1.0
 */
public class SplitFiles {
  private String infile = "";
  private FileInputStream in;
  private FileOutputStream out;
  private String outfiles = "";
  private String outfilepath = "";
  private String outfile = "";
  private long startpos = 0;
  private long endpos = 0;
  private long linelength = 0;
  private static final int BUFFERLENGTH = 2048;//缓冲区大小

  public SplitFiles() {
  }

  public void setInputFile(String file){
    this.infile = file;
  }

  public void setOutFilePath(String path){
    this.outfilepath = path;
  }

  public void split(){
    if(isAllowToSplit()){
      //首先取得合并文件的合并信息
      this.outfiles = getFilesInfo();
      for(int i = 0; i< this.outfiles.split("/").length; i++){
        this.outfile = this.outfiles.split("/")[i].toString().split(":")[0].toString();
        this.startpos = Long.parseLong(this.outfiles.split("/")[i].toString().split(":")[1].toString(),10);
        this.endpos = Long.parseLong(this.outfiles.split("/")[i].toString().split(":")[2].toString(),10);

//System.out.println(outfile + ":" + startpos + ":" + endpos);

        try {
          //创建文件输入流对象实例
          in = new FileInputStream(this.infile);

          //创建文件输出流对象实例
          out = new FileOutputStream(this.outfilepath + this.outfile);
          byte[] buffer = new byte[this.BUFFERLENGTH];
          int nNumber = 0;
          int _t_number = 0;
          in.skip(startpos+this.linelength+1);
          while ( (nNumber = in.read(buffer, 0, buffer.length)) != -1) {
            _t_number = _t_number + nNumber;
            if(_t_number > (endpos - startpos)){
              nNumber = (int)(endpos - startpos)%this.BUFFERLENGTH;
              out.write(buffer, 0, nNumber);
              break;
            }else{
              out.write(buffer, 0, nNumber);
            }
          }
          //关闭文件流对象
          out.close();
          in.close();
        }
        catch (IOException e) {
          System.err.println("IO错误");
        }
      }
    }
  }

  /**
   * 是否已初始化
   * @return boolean
   */
  private boolean isAllowToSplit(){
    boolean b = true;
    if (this.infile.equals("")) {
      System.err.println("没有设置要拆分的文件名!");
      b = false;
    }

    if (this.outfilepath.equals("")) {
      System.err.println("没有设置要拆分后文件的存放路径!");
      b = false;
    }

    File f = new File(this.infile);
    if (!f.exists()) {
      System.err.println("没有找到要拆分的文件:"+f.getPath());
      b = false;
    }
    return b;
  }

  /**
   * 取得要拆分的文件信息
   * @return String
   */
  public String getFilesInfo(){
    String Line = "";
    try {
      FileReader fr = new FileReader(this.infile);
      BufferedReader br = new BufferedReader(fr);
      Line = br.readLine();

      byte[] bytes = Line.toString().getBytes();
      this.linelength = bytes.length;

      br.close();
      fr.close();
    }
    catch (FileNotFoundException ex) {
      System.out.println("找不到文件:" + this.infile);
    }
    catch (IOException ex) {
      System.out.println("读文件:" + this.infile + "失败");
    }
    return Line;
  }


//  public static void main(String[] args) {
//    SplitFiles splitFiles1 = new SplitFiles();
//    splitFiles1.setInputFile("d://test//AA");
//    splitFiles1.setOutFilePath("d://test2//");
//    splitFiles1.split();
//  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值