这个SequenceInputStream的功能就是合并多个input输入流(继承自InputStream的类都可以合并),在看这个之前你可以先看一下我其他的I/O文章,下面我写了一个小例子,功能就是读取两个文件的内容,然后合并成一个新的文件,先贴上代码和实验结果。
import java.io.*;
public class test {
private static String a;
public static void main(String[] args) throws IOException, InterruptedException {
File file1 = new File("C:\\Users\\Administrator\\Desktop\\a.txt");
File file2 = new File("C:\\Users\\Administrator\\Desktop\\a1.txt");
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\info1\\b.txt");
FileInputStream in1=new FileInputStream(file1);
FileInputStream in2 =new FileInputStream(file2);
SequenceInputStream sequenceInputStream = new SequenceInputStream(in1,in2);
byte[] by = new byte[50];
int length=0;
while ((length=sequenceInputStream.read(by))!=-1){
fileOutputStream.write(by,0,length);
fileOutputStream.flush();
}
in1.close();
in2.close();
sequenceInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}