SequenceInputStream

本文通过一个具体示例展示了如何使用 Java 中的 SequenceInputStream 类来依次读取多个 FileInputStream 对象中的数据,并将其合并写入到一个新的文件中。示例首先创建了一个包含三个 FileInputStream 的 List,然后通过 Collections.enumeration 方法将 List 转换为 Enumeration,最后利用 SequenceInputStream 来依次读取这些流并将内容写入目标文件。

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

public class sequenceInputStreamDemo {
@Test
public void t1() throws IOException{
List<FileInputStream> list=new ArrayList<FileInputStream>();//写一个list放FileInputStream
list.add(new FileInputStream("file/sqe/1.txt"));
list.add(new FileInputStream("file/sqe/2.txt"));
list.add(new FileInputStream("file/sqe/3.txt"));

Enumeration<FileInputStream> en = Collections.enumeration(list);//※将list转换成枚举

//入口
SequenceInputStream sis = new SequenceInputStream(en);//※放入序列zhong

//流拷贝
//把序列流中的数据写到一个文件中
FileOutputStream fout = new FileOutputStream("file/sqe/a.txt");
byte b[] = new byte[8];
int len = 0;
while( (len=sis.read(b))!=-1){
fout.write(b, 0, len);
}
fout.close();
sis.close();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值