public static void listDirectory(File dir) throws IOException {
if (!dir.exists()) {
throw new IllegalArgumentException("目录" + dir + "不存在");
}
if (!dir.isDirectory()) {
throw new IllegalArgumentException(dir + "不是目录");
}
/* String[] strings=dir.list();
for (String string : strings) {
System.out.println("string = " + string);
}
System.out.println("==================================");*/
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
//递归
listDirectory(file);
} else {
System.out.println(file);
}
}
}
}
如此便可打印出目录下所有文件