SequenceInputStream example

xiaoxiao2021-04-05  267

/ *

The SequenceInputStream class allows multiple InputStream streams to be connected. The construction of SequenceInputStream is different from any other InputStream. SEQUENCEINPUTSTREAM constructor either uses a pair of InputStream, or uses an Enumeration of InputStream, as shown below:

SequenceInputStream (InputStream First, InputStream Second)

SequenceInputStream (Enumeration Streamenum)

In operation, the class satisfies the reading requirement of reading the second stream after reading the first InputStream. With Enumeration, it will continue to read all InputStream flow until the last one is read.

Text.txt and file2.txt must be in the current directory.

* /

// Demonstrate Squenced Input.

Import java.io. *;

Import java.util. *;

Class InputStreamenumerator imports enumeration {

PRIVATE ENUMERATION FILES;

Public InputStreamEnumerator (Vector files) {

THIS.FILES = files.elements ();

}

Public Boolean HasmoreElements () {

Return files.hasmorelements ();

}

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.addeElement ("text.txt"); files.addelement ("file2.txt");

InputStreamEnumerator E = New InputStreamenumerator (FILES);

InputStream INPUT = New SequenceInputStream (e);

While ((c = INPUT.READ ())! = - 1) {

System.out.print (CHAR) C);

}

INPUT.CLOSE ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-132021.html

New Post(0)