public class AccessSubDir {
public static void main(String args[]) {
GetFiles.getSubDir("D:/Wader.Lu/javawork");
}
}
class GetFiles {
public static void getSubDir(String filename) {
File dir = new File(filename);
System.out.println();
//Read all sub directorys
if(dir.isDirectory()){
String[] subFile = dir.list();
System.out.println("\""+dir.getAbsolutePath()+"\""+" is a directory! It's contents "+subFile.length+" files, include this: ");
//Read subfiles
for(int i=0; i<subFile.length; i++) {
System.out.print(subFile+"\t");
}
System.out.println("\n");
int j = 0;//Directorys counter
System.out.println("\""+dir.getAbsolutePath()+"\""+" include this directorys:");
//Show which one is directory
for(int i=0; i<subFile.length; i++) {
File subDir = new File(dir, subFile);
if(subDir.isDirectory()) {
System.out.println(subDir.getName());
j++;
}
}
System.out.println("\nTotal "+j+" directorys");
System.out.println();
//Get subdirecotry's files
for(int i=0; i<subFile.length; i++) {
File subDir = new File(dir, subFile);
if(subDir.isDirectory()) {
getSubDir(subDir.getAbsolutePath());
System.out.println();
}
}
}
//Read all subfiles
else if(dir.isFile()) {
System.out.println("\""+dir.getName()+"\""+" is a file!");
}
//The file can not read or not existed
else {
System.out.println("\""+dir.getName()+"\""+" is not existed or can not read!");
}
}
}作者: shanxi 时间: 2007-4-14 14:40
Exception都没有捕捉,这样的代码,唉!~~