文件切割与拷贝

package cn.weida.io.File;
import java.awt.event.FocusEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import org.omg.CORBA.LongSeqHelper;
import org.omg.CosNaming.BindingIteratorOperations;
import cn.weida.io.others.fileCloseUtils;
/**
 * 文件分割 第一步 确定块数 确定一块的大小 每块的名称 第二步 分割
 *
 * @author 24569
 *
 */
public class SplitFile {
 // 源文件路径
 private String srcpath;
 // 源文件名
 private String srcname;
 // 目标文件路径
 private String destDatepath;
 // 目标文件名
 private String destname;
 // 分割的块数
 private int size;
 // 每块大小
 private long blockSize;
 // 文件长度
 private long length;
 // 源文件
 File src = null;
 // 目标文件
 File dest = null;
 // 分割的中间文件
 private List<String> blockname = null;
 public SplitFile() {
  this.blockname = new ArrayList<String>();
 }
 public SplitFile(String srcpath, String destpath) {
  this(srcpath, destpath, 1024);
 }
 public SplitFile(String srcpath, String destpath, int blockSize) {
  this();
  this.srcpath = srcpath;
  this.destDatepath = destpath;
  this.blockSize = blockSize;
  init();
 }
 // 计算每块的大小
 public void init() {
  this.src = new File(this.srcpath);
  if (srcpath == null || src.isDirectory() || !(src.exists())) {
   return;
  }
  this.length = src.length();
  if (this.length < blockSize) {
   blockSize = this.length;
  }
  this.size = (int) Math.ceil((this.length * 1.0) / this.blockSize);
  this.srcname = src.getName();
  initPathname();
  dest = new File(destDatepath);
 }
 // 确定分块名称
 public void initPathname() {
  for (int i = 0; i < this.size; i++) {
   blockname.add(this.srcname + ".part" + i);
  }
 }
 // 分割
 public void split() {
  // 开始位置
  long beginPos = 0;
  // 实际大小
  long ActualSize = this.blockSize;
  for (int i = 0; i < size; i++) {
   if (i == size - 1) {
    ActualSize = length - beginPos;
   }
   beginPos += this.blockSize;
   splitDetail(i, beginPos, ActualSize);
  }
 }
 /**
  * @param i第几分块
  * @param begin
  * @param ActualSize
  * @throws FileNotFoundException
  */
 public void splitDetail(int i, long beginpos, long ActualSize) {
  // 选择流
  File destx = new File(dest.getAbsoluteFile()+"/"+this.blockname.get(i));
  BufferedOutputStream bos=null;
  RandomAccessFile raf = null;
  try {
   // 输出流
   bos= new BufferedOutputStream(new FileOutputStream(destx));
   raf = new RandomAccessFile(src, "r");
   raf.seek(beginpos);
   byte[] flush = new byte[(int) ActualSize];
   int len=0;
   while (-1!=(len=raf.read(flush))) {
    if (ActualSize-len>=0) {
     bos.write(flush, 0, len);
     ActualSize-=len;
    }
    else {
     bos.write(flush, 0, (int)ActualSize);
     break;
    }
   }
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  catch (IOException e) {
   e.printStackTrace();
  } finally {
   fileCloseUtils.close(bos,raf);
  }
 }
 // 合并
 public void mergeFile (String despath) {
  //目的文件
  File destp = new File(despath);
  BufferedOutputStream bos = null;
   try {
    bos= new BufferedOutputStream(new FileOutputStream(destp,true));
    for (int i=0;i<this.blockname.size();i++) {
     BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(dest.getAbsoluteFile()+"/"+this.blockname.get(i))));
     byte[] flush = new byte[1024];
     int len=0;
     while (-1!=(len=bis.read(flush))) {
      bos.write(flush, 0, len);
     }
     bos.flush();
     fileCloseUtils.close(bis);
    }
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    fileCloseUtils.close(bos);
   }
  
 }
 
 
 public static void main(String[] args) {
  SplitFile file = new SplitFile("C:/Users/24569/Desktop/5.txt", "C:/Users/24569/Desktop", 50);
  //file.split();
  file.mergeFile("C:/Users/24569/Desktop/6.txt");
 }
}

猜你喜欢

转载自blog.csdn.net/acm160920007/article/details/79280936
今日推荐