public class FileTest {
public static void main(String[] args) {
//==========
File[] ff=File.listRoots();
for(File fff:ff){
printName(fff);
}
}
/**
* 打印給定文件下的所有文件(這裏所有的文件都是抽象,只是個文件夾或文件名)
* 注意操作系統無訪問權限的文件,否則:java.lang.NullPointerException
* @param 由文件路徑或文件名構建的文件對象
*/
public static void printName(File f){
System.out.println(f.getName());
if(f.isFile()){
System.out.println(f.getName());
return;
}
if(f.isDirectory()){
System.out.println(f.getAbsolutePath());
//System.out.println(f.canRead());
//System.out.println(f.canWrite());
File[] rootfile=f.listFiles();
//System.out.println("rootfile");
//System.out.println(rootfile==null);
/**
* An array of File objects denoting the available filesystem roots,
* or null if the set of roots could not be determined. The array will
* be empty if there are no filesystem roots.
*/
if(null==rootfile){//比如系統驅動等文件夾,雖然裏面有內容,但系統加鎖沒訪問權限,返回個null
System.out.println(f.getAbsolutePath()+"這裏面有祕密,就是不給看");
return;
}
for(File ff:rootfile){
printName(ff);
}
}
}
}