Java/File Input Output/SequenceInputStream
Версия от 18:01, 31 мая 2010; (обсуждение)
Demonstrate sequenced input.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
class InputStreamEnumerator implements Enumeration<FileInputStream> {
private Enumeration<String> files;
public InputStreamEnumerator(Vector<String> files) {
this.files = files.elements();
}
public boolean hasMoreElements() {
return files.hasMoreElements();
}
public FileInputStream nextElement() {
try {
return new FileInputStream(files.nextElement().toString());
} catch (IOException e) {
return null;
}
}
}
class SequenceInputStreamDemo {
public static void main(String args[]) throws IOException {
int c;
Vector<String> files = new Vector<String>();
files.addElement("c:\\autoexec.bat");
files.addElement("c:\\config.sys");
InputStreamEnumerator e = new InputStreamEnumerator(files);
InputStream input = new SequenceInputStream(e);
while ((c = input.read()) != -1) {
System.out.print((char) c);
}
input.close();
}
}
Sequence IO
import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
public class SequenceIOApp {
public static void main(String args[]) throws IOException {
SequenceInputStream inStream;
FileInputStream f1 = new FileInputStream("ByteArrayIOApp.java");
FileInputStream f2 = new FileInputStream("FileIOApp.java");
inStream = new SequenceInputStream(f1, f2);
boolean eof = false;
int byteCount = 0;
while (!eof) {
int c = inStream.read();
if (c == -1)
eof = true;
else {
System.out.print((char) c);
++byteCount;
}
}
System.out.println(byteCount + " bytes were read");
inStream.close();
f1.close();
f2.close();
}
}