Java/File Input Output/SequenceInputStream — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:03, 1 июня 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();
}
}