多个碎片的合并

本文介绍如何使用Java的SequenceInputStream来合并多个文件碎片成一个完整的文件。文中提供了两种方法:一种是利用Vector结合SequenceInputStream实现;另一种是通过ArrayList提高效率,并自定义枚举器实现流的合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

多个碎片的合并

SecuenceInputStream【序列流,两个流的合并】 sic=new SecuenceInputStream(fis1,fis2);

【多个流的合并】SecuenceInputStream【枚举器对象】 sic=new SecuenceInputStream(Enumeration<InputStream> en);

1、用向量对象存放集合

Vector <FileInputStream> v=new Vector <FileInputStream>();

For(int i=1;i<=4;i++){

        v.add(“new FileInputStream(“res/”+i+”.part”)”);

}

SequenceInputStream sis=new SequenceInputStream(v.elements());

FileOutputStream fos=new FileOutputStream(“1.mp3”);

Byte[] buf=new byte[1024*4];

Int len=0;

While((len=sis.read(buf))!=-1){

        Fos.write(bus,0,len);

}

2、因为要提高效率所以用ArrayList

ArrayList<FileInputStream> alist=new ArrayList<FileInputStream>();

For(int i=1;i<=6;i++){

        Alist.add(new FileInputStream(i+”.part”));

}

Final Iterator【接口】 <FileInputStream> it【迭代器对象】=new alist.iterator();

Class MyEnum implements Enumeration<FileInputStream>{

        public boolean hasMoreElements(){

               return it.hasNext();

}

public FileInputStream nextElements(){

       return it..next();

}

MyEnum en=new MyEnum;

SequenceInputStream sis=new SequenceInputStream(en);

FileOutputStream fos=new FileOutputStream(“1.mp3”);

Byte[] buf=new byte[1024*4];

Int len=0;

While((len=sis.read(buf))!=-1){

        Fos.write(bus,0,len);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值