Skip to content

Commit f7c89f0

Browse files
BAEL-9201: Count files in directory (#18436)
* initial commit * Remove unnecessary code and dependencies * Rename file to end with *UnitTest
1 parent 6cbc80d commit f7c89f0

File tree

7 files changed

+133
-0
lines changed

7 files changed

+133
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.baeldung.javafeatures;
2+
3+
import static java.io.IO.println;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.Arrays;
11+
import java.util.Optional;
12+
import java.util.function.Function;
13+
import java.util.stream.Stream;
14+
15+
public class FindFolder {
16+
17+
public static long numberOfFilesIn_classic(String path) {
18+
File currentFile = new File(path);
19+
File[] filesOrNull = currentFile.listFiles();
20+
long currentFileNumber = currentFile.isFile() ? 1 : 0;
21+
22+
if (filesOrNull == null) {
23+
return currentFileNumber;
24+
}
25+
26+
for (File file : filesOrNull) {
27+
if (file.isDirectory()) {
28+
currentFileNumber += numberOfFilesIn_classic(file.getAbsolutePath());
29+
} else if (file.isFile()) {
30+
currentFileNumber += 1;
31+
}
32+
}
33+
34+
return currentFileNumber;
35+
}
36+
37+
public static long numberOfFilesIn_Stream(String path) {
38+
File currentFile = new File(path);
39+
File[] filesOrNull = currentFile.listFiles();
40+
long currentFileNumber = currentFile.isFile() ? 1 : 0;
41+
42+
if (filesOrNull == null) {
43+
return currentFileNumber;
44+
}
45+
46+
return currentFileNumber + Arrays.stream(filesOrNull)
47+
.mapToLong(FindFolder::filesInside)
48+
.sum();
49+
}
50+
51+
private static long filesInside(File it) {
52+
if (it.isFile()) {
53+
return 1;
54+
} else if (it.isDirectory()) {
55+
return numberOfFilesIn_Stream(it.getAbsolutePath());
56+
} else {
57+
return 0;
58+
}
59+
}
60+
61+
public static long numberOfFilesIn_Walk(String path) {
62+
Path dir = Path.of(path);
63+
try (Stream<Path> stream = Files.walk(dir)) {
64+
return stream.parallel()
65+
.map(getFileOrEmpty())
66+
.flatMap(Optional::stream)
67+
.filter(it -> !it.isDirectory())
68+
.count();
69+
} catch (IOException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
private static Function<Path, Optional<File>> getFileOrEmpty() {
75+
return it -> {
76+
try {
77+
return Optional.of(it.toFile());
78+
} catch (UnsupportedOperationException e) {
79+
println(e);
80+
return Optional.empty();
81+
}
82+
};
83+
}
84+
85+
public static long numberOfFilesIn_NIO(String path) {
86+
try (Stream<Path> stream = Files.find(
87+
Paths.get(path),
88+
Integer.MAX_VALUE,
89+
(__, attr) -> attr.isRegularFile())
90+
) {
91+
return stream.count();
92+
} catch (IOException e) {
93+
throw new RuntimeException(e);
94+
}
95+
}
96+
}
97+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.javafeatures;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.function.Function;
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
public class NumberOfFilesUnitTest {
12+
13+
private final String resourcePath = this.getClass()
14+
.getResource("/filesToBeFound")
15+
.getPath();
16+
17+
@ParameterizedTest
18+
@MethodSource("functionsUnderTest")
19+
void shouldReturnNumberOfAllFilesInsidePath(Function<String, Long> functionUnderTest) {
20+
long expectedCount = 5;
21+
22+
long result = functionUnderTest.apply(resourcePath);
23+
24+
assertThat(result).isEqualTo(expectedCount);
25+
}
26+
27+
private static Stream<Arguments> functionsUnderTest() {
28+
return Stream.<Function<String, Long>> of(
29+
FindFolder::numberOfFilesIn_Walk,
30+
FindFolder::numberOfFilesIn_classic,
31+
FindFolder::numberOfFilesIn_Stream,
32+
FindFolder::numberOfFilesIn_NIO
33+
)
34+
.map(Arguments::of);
35+
}
36+
}

core-java-modules/core-java-23/src/test/resources/filesToBeFound/file3.txt

Whitespace-only changes.

core-java-modules/core-java-23/src/test/resources/filesToBeFound/subFolder1/file1

Whitespace-only changes.

core-java-modules/core-java-23/src/test/resources/filesToBeFound/subFolder1/file2

Whitespace-only changes.

core-java-modules/core-java-23/src/test/resources/filesToBeFound/subFolder2/file4

Whitespace-only changes.

core-java-modules/core-java-23/src/test/resources/filesToBeFound/subFolder2/subSubFolder/file5

Whitespace-only changes.

0 commit comments

Comments
 (0)