读取文件夹中文件
public static List readfile(String filepath) throws FileNotFoundException, IOException {
List list = new ArrayList();
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath=" + readfile.getAbsolutePath()); System.out.println("name=" + readfile.getName());
list.add(readfile);
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage()); }
return list;
}
//调用该方法即可
public static void main(String args[]){
String filePath = "E:\\practise";
List list = new ArrayList();
Map map = null;
list = MessageUtil.readfile(filePath);
//其中list中包含了该文件夹中的所有文件。
}