//统计文件夹下的文件,是文件就累计1,隐藏文件空累计,文件不累计
public static void main(String[]args) {
System.out.println(getFileNumber("D:\重要文件"));
}
public static int getFileNumber(String folderPath) {
File root = new File(folderPath);
if(!root.isDirectory()&&!root.isFile()) {
return 0;
}
if (root.isFile()) {
return 1;
}
Stack
stack.add(root);
int files =0;
while(!stack.isEmpty()) {
File folder = stack.pop();
for(File next : folder.listFiles()) {
if(next.isFile()) {
files++;
}
if(next.isDirectory()) {
stack.push(next);
}
}
}
return files;
}