重修大学JAVA课:File.io(一)

来源:互联网 发布:windows徽标 编辑:程序博客网 时间:2024/04/27 18:08
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);
}
}
}
}
0 0