File's tree traversal

Report a typo

Here's one way to traverse all subdirectories and collect all files in a list.

The method listFiles returns the nested files and directories of a directory as an array.

1  public List<File> getAllFiles(File rootDir) {
2      File[] children = rootDir.listFiles();
3      if (children == null || children.length == 0) {
4          return Collections.emptyList();
5      }
6
7      List<File> files = new ArrayList<>();
8      for (File child : children) {
9          if (child.__) {
10             files.addAll(getAllFiles(child));
11         } else {
12             files.add(child);
13         }
14     }
15
16     return files;
17 }

Replace the underscore with correct code (line 9) to make the code correct: it should collect all files in a current directory and its subdirectories.

Enter a short text
___

Create a free account to access the full topic