Listing all files with a .txt extension recursively

61 Views Asked by At

I am trying to recursively search the directory and list all .txt files found. This is my code for it:

private static void listFilesForFolder(File folder) throws FileNotFoundException {
for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
        } else {
            System.out.println(Arrays.toString(fileEntry.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(".txt");
                }
            })));
        }
    }
}

I'm using FileFilter to print out all the .txt files but it prints out null instead. Anyone know why that's the case?

2

There are 2 best solutions below

3
Prasad Karunagoda On

I think instead of using a FileFilter in the else block, you can simply use an if statement. Because in this else block you always have a file (not a directory). See whether below change works for you.

private static void listFilesForFolder(File folder) throws FileNotFoundException
{
  for (final File fileEntry : folder.listFiles()) {
    if (fileEntry.isDirectory()) {
      listFilesForFolder(fileEntry);
    } else {
      if (fileEntry.getPath().toLowerCase().endsWith(".txt")) {
        System.out.println(fileEntry.getPath());
      }
      /*System.out.println(Arrays.toString(fileEntry.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
          return pathname.getName().endsWith(".txt");
        }
      })));*/
    }
  }
}

EDIT:
If you really want to do this using FileFilter, you can do it like this:

private static void listFilesForFolder2(File folder) throws FileNotFoundException
{
  File[] textFiles = folder.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
      return pathname.isFile() && pathname.getName().toLowerCase().endsWith(".txt");
    }
  });
  if (textFiles != null) {
    for (File f : textFiles) {
      System.out.println(f.getPath());
    }
  }

  for (final File fileEntry : folder.listFiles()) {
    if (fileEntry.isDirectory()) {
      listFilesForFolder2(fileEntry);
    }
  }
}
0
TongChen On

The method file.listFiles() return String[] only when the file object is a Directory object,if file is a File object it will return null. You can just modify you code like this:

for (final File fileEntry : folder.listFiles()) {
    if (fileEntry.isDirectory()) {
        listFilesForFolder(fileEntry);
    } else {
        if (fileEntry.getName().endsWith(".txt")) {
            System.out.println(fileEntry.getName());
        }
    }
}

if you can use apache commons-io it will be easy to use IOFileFilter to filter what you want:

    FileUtils.listFiles(folder, new IOFileFilter() {
        @Override
        public boolean accept(File file) {
            return file.getName().endsWith(".txt");
        }

        @Override
        public boolean accept(File file, String s) {
            return true;
        }
    }, new IOFileFilter() {
        @Override
        public boolean accept(File file) {
            return true;
        }

        @Override
        public boolean accept(File file, String s) {
            return true;
        }
    }).stream().forEach(file -> System.out.println(file.getName()));