1.统计目录下的文件数
public static int count(String route){
File root = new File(route);
if (root.isFile()){
return 1;
}
if (!root.isDirectory()){
return 0;
}
Stack<File> s = new Stack<>();
s.push(root);
int ans = 0;
while (!s.isEmpty()){
File cur = s.pop();
for (File f : cur.listFiles()){
if (f.isFile()){
ans++;
}else if (f.isDirectory()){
s.push(f);
}
}
}
return ans;
}
public static void main(String[] args) {
String path = "C:\\Users\\yu\\Desktop\\leetcode";
System.out.println(count(path));
}