import java.io.*;
import java.util.*;
public class Text {
public static void main(String[] args) throws Exception {
ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
list.add(new FileInputStream("c:\\1.txt"));
list.add(new FileInputStream("c:\\2.txt"));
list.add(new FileInputStream("c:\\3.txt"));
Enumeration<FileInputStream> en=enumerationShow(list);
while (en.hasMoreElements()) {
String readResult=readArrarListFile(en.nextElement());
System.out.println(readResult);
}
}
private static Enumeration<FileInputStream> enumerationShow(ArrayList<FileInputStream> list) throws Exception {
final Iterator<FileInputStream> iter = list.iterator(); //通过ArrayList的迭代器来实现访问集合 中的元素
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() {//此处使用了匿名内部类,并且实现了借口中的抽象方法
public boolean hasMoreElements() {
return iter.hasNext();
}
public FileInputStream nextElement(){
return iter.next();
}
};
return en;
}
//目的是读出每个file里的内容
public static String readArrarListFile(FileInputStream file) throws IOException{
String str=null,line=null;
BufferedReader br=new BufferedReader(new InputStreamReader(file));
while ((line=br.readLine())!=null) {
str+=line+"\n";
}
return str;
}
}
|