public class FileUtils {
public static void listDirectory(File dir) throws IOException{
if(!dir.exists()){
throw new IllegalArgumentException("目录:"+dir+"不存在。");
}
if(!dir.isDirectory()){
throw new IllegalArgumentException(dir+"不是目录");
}
//返回的是字符串數組,直接是子的名稱。
/*String[] filenames=dir.list();
for(String s:filenames){
System.out.println(s);
}*/
//如果要遍历子目录下的内容,就需要构造成file对象做递归
File[] files=dir.listFiles();
if(files!=null&&files.length>0){
for(File file:files){
if(file.isDirectory()){
listDirectory(file);
}else {
System.out.println(file);
}
}
}
}
public static void main(String[] args) throws IOException{
listDirectory(new File("G:\\VC++"));
}
}