Java IO流文件切割和合并

文件切割器+合并器

CutAndCombinFime.java 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

 public class CutAndCombinFile {

   private static final int SIZE = 1024 * 1024;

   public static void main(String[] args) throws IOException {
      File file = new File("C:\\partfiles");
      // cutFile(file);
      combinFiles(file);
   }

   public static void cutFile(File file) throws IOException {
      FileInputStream fis = new FileInputStream("C:\\一人一花.mp3");
      byte buf[] = new byte[SIZE];
      int len = 0;
      int count = 0;
      FileOutputStream fos = null;
      if (!file.exists())
        file.mkdirs();
      while ((len = fis.read(buf)) != -1) {
        fos = new FileOutputStream(new File(file, ++count + ".part"));
        fos.write(buf, 0, len);
        fos.flush();
      }
      fos.close();
      fis.close();
   }
   public static void combinFiles(File file) throws IOException {
      ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
      for (int x = 1; x <= 12; x++) {
        al.add(new FileInputStream(new File(file, x + ".part")));
      }
      Enumeration<FileInputStream> en = Collections.enumeration(al);
      SequenceInputStream sis = new SequenceInputStream(en);
      FileOutputStream fos = new FileOutputStream(new File(file, "合并.mp3"));
      byte[] buf = new byte[1024];
      int len = 0;
      while ((len = sis.read(buf)) != -1) {
        fos.write(buf, 0, len);
      }
      fos.close();
      sis.close();
   }
}