[Java基础] SequenceInputStream输入合并流
转载: http://blog.csdn.net/xuefeng1009/article/details/6955707
public SequenceInputStream(Enumeration<? extends InputStream> e)
- 通过记住参数来初始化新创建的
SequenceInputStream
,该参数必须是生成运行时类型为InputStream
对象的Enumeration
型参数。将按顺序读取由该枚举生成的输入流,以提供从此SequenceInputStream
读取的字节。在用尽枚举中的每个输入流之后,将通过调用该流的close
方法将其关闭。- 参数:
e
- 输入流的一个枚举。- 另请参见:
Enumeration
SequenceInputStream
public SequenceInputStream(InputStream s1, InputStream s2)
- 通过记住这两个参数来初始化新创建的
SequenceInputStream
(将按顺序读取这两个参数,先读取s1
,然后读取s2
),以提供从此SequenceInputStream
读取的字节。- 参数:
s1
- 要读取的第一个输入流。s2
- 要读取的第二个输入流。- 举例 1 :
import java.io.*; import java.util.*; class SequenceDemo1 { public static void main(String[] args)throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("c:\\1.txt")); v.add(new FileInputStream("c:\\2.txt")); v.add(new FileInputStream("c:\\3.txt")); Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("c:\\4.txt"); byte[] buf = new byte[1024]; int len = 0; while((len=sis.read(buf))!=-1) { fos.write(buf,0,len); } fos.close(); sis.close(); } }
举例 2 :
-
import java.io.*; import java.util.*; class SequenceDemo2 { public static void main(String[] args)throws IOException { InputStream is1 = null; InputStream is2 = null; OutputStream os = null; SequenceInputStream sis = new null; is1 = new FileInputStream("d:"+File.separator+"a.txt"); is2 = new FileInputStream("d:"+File.separator+"b.txt"); os = new FileOutputStream("d:"+File.separator+"ab.txt"); sis = new SequenceInputStream(is1,is2); int temp = 0; while((temp)=sis.read()!=-1) { os.write(temp); } sis.close(); is1.close(); is2.close(); os.close(); } }