A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© xiaoxinxin003 中级黑马   /  2015-7-10 11:37  /  596 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

合并流:SequenceInputStream将多个流合并成一个流
        切割流:SplitFile
  1. import java.io.*;
  2. import java.util.*;
  3. class SplitFile {
  4.         public static void main(String[] args) throws IOException{
  5.                 merge();
  6.         }
  7.         //合并************************//
  8.         public static void merge()throws IOException{
  9.                 //创建流对象并导入源文件
  10.                 ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
  11.                 for(int x=1;x<=4;x++){
  12.                         al.add(new FileInputStream("e:\\SplitFiles\\"+x+".part"));
  13.                 }
  14.                 //对返回的局部变量进行final修饰
  15.                 final Iterator<FileInputStream> it = al.iterator();
  16.                 //匿名内部类 对返回的局部变量进行final修饰
  17.                 Enumeration<FileInputStream> en=new Enumeration<FileInputStream>(){
  18.                         public boolean hasMoreElements(){
  19.                                 return it.hasNext();
  20.                         }
  21.                         public FileInputStream nextElement(){
  22.                                 return it.next();
  23.                         }
  24.                 };
  25.                 //创建合并流对象
  26.                 SequenceInputStream sis=new SequenceInputStream(en);
  27.                 //指定合并目的文件及路径
  28.                 FileOutputStream fos=new FileOutputStream("e:\\zijinhuayuan.mp3");
  29.                 byte[] buf=new byte[1024*1024*5];
  30.                 int len=0;
  31.                 while((len=sis.read(buf))!=-1){
  32.                         fos.write(buf,0,len);
  33.                 }
  34.                 fos.close();
  35.                 sis.close();
  36.         }
  37.         //切割*****************************//
  38.         public static void splitFile()throws IOException{
  39.                 FileInputStream fis=new FileInputStream("e:\\紫金花园.mp3");
  40.                 FileOutputStream fos=null;
  41.                 byte[] buf=new byte[1024*1024*3];
  42.                 int len=0;
  43.                 int count=1;
  44.                 while((len=fis.read(buf))!=-1){//指定路径和名字
  45.                         fos=new FileOutputStream("e:\\SplitFiles\\"+(count++)+".part");
  46.                         fos.write(buf,0,len);
  47.                         fos.close();
  48.                 }
  49.                 fis.close();
  50.         }
  51. }
复制代码




1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马