IO流中SequenceInputStream类
SequenceInputStream类:
不断的读取InputStream流对象,对于使用Enumeration对象的情况,该类将持续读取所有InputStream对象中的内容,直到到达最后一个InputStream对象的末尾为止
当到达每个文件的末尾时,与之关联的流就会被关闭。关闭通过SequenceInputStream创建的流,会导致关闭所有未关闭的流
package com.test; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; import org.junit.Test; /** * @Description:IO的Dmeo * @author zbs * @date 2016年11月24日 下午1:47:02 */ public class IODemo { @Test public void test() { // 创建一个合并流对象 SequenceInputStream sis = null; // 创建输出流 BufferedOutputStream bos = null; // 构造输入流集合 Vector<InputStream> vector = new Vector<InputStream>(); try { vector.addElement(new FileInputStream(new File("E:/1.txt"))); vector.addElement(new FileInputStream(new File("E:/2.txt"))); vector.addElement(new FileInputStream(new File("E:/3.txt"))); vector.addElement(new FileInputStream(new File("E:/4.txt"))); Enumeration<InputStream> enums = vector.elements(); sis = new SequenceInputStream(enums); bos = new BufferedOutputStream(new FileOutputStream(new File("E:/all.txt"))); byte[] buff =new byte[1024]; int len = 0; while((len = sis.read(buff)) != -1) { bos.write(buff, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if(sis != null) { try { sis.close(); } catch (IOException e) { e.printStackTrace(); } finally { if(bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } }