重修大學JAVA課:File.io(一)

import java.io.File;


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);
}
}
}
}
發佈了30 篇原創文章 · 獲贊 1 · 訪問量 6184
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章