合并流:SequenceInputStream将多个流合并成一个流
切割流:SplitFile
- import java.io.*;
- import java.util.*;
- class SplitFile {
- public static void main(String[] args) throws IOException{
- merge();
- }
- //合并************************//
- public static void merge()throws IOException{
- //创建流对象并导入源文件
- ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
- for(int x=1;x<=4;x++){
- al.add(new FileInputStream("e:\\SplitFiles\\"+x+".part"));
- }
- //对返回的局部变量进行final修饰
- final Iterator<FileInputStream> it = al.iterator();
- //匿名内部类 对返回的局部变量进行final修饰
- Enumeration<FileInputStream> en=new Enumeration<FileInputStream>(){
- public boolean hasMoreElements(){
- return it.hasNext();
- }
- public FileInputStream nextElement(){
- return it.next();
- }
- };
- //创建合并流对象
- SequenceInputStream sis=new SequenceInputStream(en);
- //指定合并目的文件及路径
- FileOutputStream fos=new FileOutputStream("e:\\zijinhuayuan.mp3");
- byte[] buf=new byte[1024*1024*5];
- int len=0;
- while((len=sis.read(buf))!=-1){
- fos.write(buf,0,len);
- }
- fos.close();
- sis.close();
- }
- //切割*****************************//
- public static void splitFile()throws IOException{
- FileInputStream fis=new FileInputStream("e:\\紫金花园.mp3");
- FileOutputStream fos=null;
- byte[] buf=new byte[1024*1024*3];
- int len=0;
- int count=1;
- while((len=fis.read(buf))!=-1){//指定路径和名字
- fos=new FileOutputStream("e:\\SplitFiles\\"+(count++)+".part");
- fos.write(buf,0,len);
- fos.close();
- }
- fis.close();
- }
- }
复制代码
|
|