The SequenceInputStream class allows you to concatenate multiple InputStreams. The construction of aSequenceInputStream is different from any other InputStream. A SequenceInputStream constructor uses either a pair of InputStreams or an Enumeration of InputStreams as its argument:

SequenceInputStream(InputStream first, InputStream second
SequenceInputStream(Enumeration streamEnum)

Operationally, the class fulfills read requests from the first InputStream until it runs out and then switches over to the second one. In the case of an Enumeration, it will continue through all of theInputStreams until the end of the last one is reached. Here is a simple example that uses aSequenceInputStream to output the contents of two files:

// Demonstrate sequenced input. 
import java.io.*; 
import java.util.*; 
class InputStreamEnumerator implements Enumeration { 
private Enumeration files; 
public InputStreamEnumerator(Vector files) { 
this.files = files.elements(); 

public boolean hasMoreElements() { 
return files.hasMoreElements(); 

public Object nextElement() { 
try { 
return new FileInputStream(files.nextElement().toString()); 
} catch (Exception e) { 
 


return null; 



class SequenceInputStreamDemo { 
public static void main(String args[]) throws Exception { 
int c; 
Vector files = new Vector(); 
files.addElement("/autoexec.bat"); 
files.addElement("/config.sys"); 
InputStreamEnumerator e = new InputStreamEnumerator(files); 
InputStream input = new SequenceInputStream(e); 
while ((c = input.read()) != -1) { 
System.out.print((char) c); 

input.close(); 

}

This example creates a Vector and then adds two filenames to it. It passes that vector of 
names to the InputStreamEnumerator class, which is designed to provide a wrapper on 
the vector where the elements returned are not the filenames but rather open 
FileInputStream
s on those names. The SequenceInputStream opens each file in turn, 
and this example prints the contents of the two files.

'자바 > 자바팁' 카테고리의 다른 글

NIO가 무엇일까요?  (1) 2012.01.18
자바 NIO 강좌  (0) 2012.01.18
자바 압축 관련 정보  (0) 2012.01.10
JSP 및 JAVA로 오직 POST 방식으로 받을때  (0) 2011.12.13
제너릭(generic) 이해하기  (0) 2011.12.06

+ Recent posts