SequenceInputStream合并流

SequenceInputStream

         表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,
接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。 

构造方法:

      1、SequenceInputStream(Enumeration<? extends InputStream> e)   合并多个文件

           因为合并多个文件,所以用到序列流的构造方法需要用到Enumeration;所以用到集合Vector

      2、SequenceInputStream(InputStream s1, InputStream s2)  合并两个文件

合并两个文件,直接把两个文件的输入流作为参数传给合并就就可以,所以下面代码演示多个文件合并为一个文件;

 

package IO;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;


public class SequenceDemo {


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Vector<FileInputStream> vc = new Vector<FileInputStream>();

//给集合中加对象
vc.add(new FileInputStream("d:\\2.txt"));
vc.add(new FileInputStream("d:\\3.txt"));
vc.add(new FileInputStream("d:\\4.txt"));

Enumeration<FileInputStream> en = vc.elements();

SequenceInputStream sis = new  SequenceInputStream(en);

FileOutputStream  fos = new FileOutputStream("d:\\88.txt");

byte[] bt = new byte[1024];
int len = 0 ;
while ((len=sis.read(bt))!=-1)
{    fos.write(bt, 0, len);
    fos.flush();
}
sis.close();

fos.close();


}

下面的程序是一个完整的把文件先切割后合并的程序

/**
 * 先把文件切割,再合并
 * 切割文件没有专门的流,自己切
 */
package IO;


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


import javax.sound.midi.Sequence;


public class SplitAndSequence {
public static void main(String[] args) throws IOException
{
//File file = new File("d://初雪.mp3");
//split(file);
merge();
}

//把文件切割
public static void split(File file) throws IOException
{
//传入要切的文件
FileInputStream is = new FileInputStream(file);
//把文件按一兆的大小切
byte[] bt = new byte[1024*1024];
FileOutputStream fos = null;
int count = 0;
int len = 0;
while((len=is.read(bt))!=-1)
{
//每次定义一个新文件,接受切割的一兆大小的文件
fos = new FileOutputStream("e:\\IO\\"+(count++)+".part");
fos.write(bt, 0, len);
// 关闭,下次循环建立新文件
fos.close();
}

}
//merge方法把文件合并
public static void merge() throws IOException{
//因为合并多个文件,所以用到序列流的构造方法需要用到Enumeration;所以用到集合Vector,因效率低,用ArrayList
ArrayList<FileInputStream> arr = new ArrayList<FileInputStream>();
for(int x=0; x<=3; x++)
{
arr.add(new FileInputStream("e:\\IO\\"+x+".part"));
  
}

final Iterator<FileInputStream> it = arr.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
{
public boolean hasMoreElements(){
return it.hasNext();
}
public FileInputStream nextElement(){
return it.next();
}
};

SequenceInputStream se = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream("e:\\初雪.mp3");

byte[] bt = new byte[1024];
int len = 0;

while((len= se.read(bt))!=-1)
{
fos.write(bt, 0, len);
fos.flush();

}
se.close();
fos.close();

}



}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值