我正在尝试使用该程序来计算 D:\ 驱动器中的所有文件,但是当我运行它时它会引发异常。
package lmh;
import java.io.File;
public class FileList {
static int fileNum = 0;
static int directoryNum = 0;
static int cannotRead = 0;
public static void main(String[] args) {
File f = new File("e:/");
printFileStructure(f);
System.out.println("result:");
System.out.println("file number:" + fileNum);
System.out.println("directory number:" + directoryNum);
System.out.println("cannot rend:" + cannotRead);
}
public static void printFileStructure(File f) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
if (files[i].canRead()) {
fileNum++;
System.out.println(files[i].getName());
} else {
cannotRead ++;
}
}
else if (files[i].isDirectory()) {
if (files[i].canRead()) {
directoryNum++;
printFileStructure(files[i]);
} else {
cannotRead ++;
}
}
}
}
}