题目
给定一个文件目录的路径,统计这个目录下所有的文件数并返回
分析
这个问题很容易想到深度搜索或广泛搜索。它不仅检查搜索算法的应用,而且检查文件的操作。这个问题在面试中很常见
[En]
This question is easy to think of deep search or extensive search. It not only examines the application of search algorithms, but also examines the operation of files. This question is very common in interviews
直接看代码吧,边看注释边讲。
代码
深度搜素:
```java
public static int dfs(String folderPath) {
File root = new File(folderPath);
// 如果传过来的参数不是目录,直接返回 0
if (!root.isDirectory()) {
return 0;
}