SequenceInputStream类的作用是将多个输入流进行逻辑上串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
如:存在三个InputStream输入流A、B、C,分别设置到SequenceInputStream中,然后开始从A输入流开始读取,直到将C读取完成为止。如下图:

实现代码如下:
package io.inputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
public class SequenceInputStreamTest {
public static void main(String[] args) throws IOException {
String buf01 = "https://";
String buf02 = "www.";
String buf03 = "bug315.com";
// 定义两个字节数组输入流
ByteArrayInputStream input1 = new ByteArrayInputStream(buf01.getBytes());
ByteArrayInputStream input2 = new ByteArrayInputStream(buf02.getBytes());
ByteArrayInputStream input3 = new ByteArrayInputStream(buf03.getBytes());
// 使用SequenceInputStream将输入流input1、input2、input3串联起来
SequenceInputStream sequence1 = new SequenceInputStream(input1, input2);
SequenceInputStream sequence2 = new SequenceInputStream(sequence1, input3);
byte[] bytes = new byte[2048];
int len = -1;
while ( (len = sequence2.read(bytes)) != -1 ) {
System.out.print( new String(bytes, 0, len) );
}
System.out.println();
}
}输出结果:
https://www.hxstrive.com