FileUtils.iterateFiles also lists directories.
MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -2231,7 +2231,7 @@ private static AccumulatorPathVisitor listAccumulate(final File directory, final
*/
public static Collection<File> listFiles(final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter) {
try {
- final AccumulatorPathVisitor visitor = listAccumulate(directory, fileFilter, dirFilter, FileVisitOption.FOLLOW_LINKS);
+ final AccumulatorPathVisitor visitor = listAccumulate(directory, FileFileFilter.INSTANCE.and(fileFilter), dirFilter, FileVisitOption.FOLLOW_LINKS);
return visitor.getFileList().stream().map(Path::toFile).collect(Collectors.toList());
} catch (final IOException e) {
throw UncheckedIOExceptions.create(directory, e);

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTest.java
src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -155,6 +155,9 @@ public long length() {
*/
private static final ListDirectoryWalker LIST_WALKER = new ListDirectoryWalker();
+ /**
+ * The test temporary folder managed by JUnit.
+ */
@TempDir
public File temporaryFolder;
@@ -1804,6 +1807,31 @@ public void testIsRegularFile() throws IOException {
assertFalse(FileUtils.isRegularFile(testFile1));
}
+ @Test
+ public void testIterateFilesOnlyNoDirs() throws IOException {
+ final File directory = temporaryFolder;
+ assertTrue(new File(directory, "TEST").mkdir());
+ assertTrue(new File(directory, "test.txt").createNewFile());
+
+ final IOFileFilter filter = new WildcardFileFilter("*", IOCase.INSENSITIVE);
+ for (final Iterator<File> itFiles = FileUtils.iterateFiles(directory, filter, null); itFiles.hasNext();) {
+ final File file = itFiles.next();
+ assertFalse(file.isDirectory(), () -> file.getAbsolutePath());
+ }
+ }
+
+ @Test
+ public void testListFilesOnlyNoDirs() throws IOException {
+ final File directory = temporaryFolder;
+ assertTrue(new File(directory, "TEST").mkdir());
+ assertTrue(new File(directory, "test.txt").createNewFile());
+
+ final IOFileFilter filter = new WildcardFileFilter("*", IOCase.INSENSITIVE);
+ for (final File file : FileUtils.listFiles(directory, filter, null)) {
+ assertFalse(file.isDirectory(), () -> file.getAbsolutePath());
+ }
+ }
+
@Test
public void testIterateFiles() throws Exception {
final File srcDir = temporaryFolder;
@@ -1940,8 +1968,7 @@ public void testListFiles() throws Exception {
}
}
- final Collection<File> actualFiles = FileUtils.listFiles(subDir, new WildcardFileFilter("*.*"),
- new WildcardFileFilter("*"));
+ final Collection<File> actualFiles = FileUtils.listFiles(subDir, new WildcardFileFilter("*.*"), new WildcardFileFilter("*"));
final int count = actualFiles.size();
final Object[] fileObjs = actualFiles.toArray();

[IO-750] FileUtils.iterateFiles also lists directories.