Commons IO
  1. Commons IO
  2. IO-173

FileUtils.listFiles() doesn't return directories

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.4
    • Fix Version/s: 2.2
    • Component/s: Utilities
    • Labels:
      None

      Description

      FileUtils.listFiles() returns only files and not directories.
      So it can't be used to retrieve sub-directories.

      Some fix could be applied:

       private static void innerListFiles(Collection files, File directory,
                  IOFileFilter filter) {
              File[] found = directory.listFiles((FileFilter) filter);
              if (found != null) {
                  for (int i = 0; i < found.length; i++) {
                      if (found[i].isDirectory()) {
      >>> fix
                          if ( addDirectories ) {
                             files.add(found[i]);
                          }
      >>> end fix
                          innerListFiles(files, found[i], filter);
                      } else {
                          files.add(found[i]);
                      }
                  }
              }
          }
      
      1. IO-173.patch
        4 kB
        Marcos Vinícius da Silva
      2. IO-173.patch
        10 kB
        Marcos Vinícius da Silva

        Activity

        Hide
        Gary Gregory added a comment -

        Version 2.2 has been released and addresses this issue.

        Show
        Gary Gregory added a comment - Version 2.2 has been released and addresses this issue.
        Hide
        Sebb added a comment -

        Thanks.

        Had to fix a bug in the code.
        The directory was being added regardless of the includeSubDirectories setting.

        Also moved the storage of the starting directory outside the recursive loop as that saves constant checking if the directory had already been added.

        Show
        Sebb added a comment - Thanks. Had to fix a bug in the code. The directory was being added regardless of the includeSubDirectories setting. Also moved the storage of the starting directory outside the recursive loop as that saves constant checking if the directory had already been added.
        Hide
        Marcos Vinícius da Silva added a comment -

        I changed the path, so that it uses the first option mentioned above: create a new method listFilesAndDirs, which is like listFiles, but include the subdirectories themselves.
        Also added an equivalent iterateFilesAndDirs.

        Show
        Marcos Vinícius da Silva added a comment - I changed the path, so that it uses the first option mentioned above: create a new method listFilesAndDirs, which is like listFiles, but include the subdirectories themselves. Also added an equivalent iterateFilesAndDirs.
        Hide
        Sebb added a comment -

        Cannot change the behaviour of an existing method - that could break lots of applications.

        except to fix a bug, which this is not.

        Show
        Sebb added a comment - Cannot change the behaviour of an existing method - that could break lots of applications. except to fix a bug, which this is not.
        Hide
        Marcos Vinícius da Silva added a comment - - edited

        I'm working on a path for this issue. Should I include a new method which lists the directories too, or keep as is, and just change the java docs to point the change?
        Something like that:

        public static Collection<File> listFilesAndDirs(
                 File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) {
            ...
        }
        

        with the proper java docs.
        Actually the patch is with the second option.

        Show
        Marcos Vinícius da Silva added a comment - - edited I'm working on a path for this issue. Should I include a new method which lists the directories too, or keep as is, and just change the java docs to point the change? Something like that: public static Collection<File> listFilesAndDirs( File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) { ... } with the proper java docs. Actually the patch is with the second option.
        Hide
        Sebb added a comment -

        Fix layout

        Show
        Sebb added a comment - Fix layout

          People

          • Assignee:
            Unassigned
            Reporter:
            François Loison
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development