File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed
core-java-modules/core-java-io-2/src
main/java/com/baeldung/listfiles
test/java/com/baeldung/listfiles Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -24,9 +24,20 @@ public Set<String> listFilesUsingJavaIO(String dir) {
2424 .collect (Collectors .toSet ());
2525 }
2626
27+ public Set <String > listFilesUsingFilesList (String dir ) throws IOException {
28+ try (Stream <Path > stream = Files .list (Paths .get (dir ))) {
29+ return stream
30+ .filter (file -> !Files .isDirectory (file ))
31+ .map (Path ::getFileName )
32+ .map (Path ::toString )
33+ .collect (Collectors .toSet ());
34+ }
35+ }
36+
2737 public Set <String > listFilesUsingFileWalk (String dir , int depth ) throws IOException {
2838 try (Stream <Path > stream = Files .walk (Paths .get (dir ), depth )) {
29- return stream .filter (file -> !Files .isDirectory (file ))
39+ return stream
40+ .filter (file -> !Files .isDirectory (file ))
3041 .map (Path ::getFileName )
3142 .map (Path ::toString )
3243 .collect (Collectors .toSet ());
Original file line number Diff line number Diff line change @@ -25,10 +25,15 @@ public class ListFilesUnitTest {
2525 };
2626
2727 @ Test
28- public void givenDir_whenUsingJAVAIO_thenListAllFiles () throws IOException {
28+ public void givenDir_whenUsingJAVAIO_thenListAllFiles () {
2929 assertEquals (EXPECTED_FILE_LIST , listFiles .listFilesUsingJavaIO (DIRECTORY ));
3030 }
3131
32+ @ Test
33+ public void givenDir_whenUsingFilesList_thenListAllFiles () throws IOException {
34+ assertEquals (EXPECTED_FILE_LIST , listFiles .listFilesUsingFilesList (DIRECTORY ));
35+ }
36+
3237 @ Test
3338 public void givenDir_whenWalkingTree_thenListAllFiles () throws IOException {
3439 assertEquals (EXPECTED_FILE_LIST , listFiles .listFilesUsingFileWalk (DIRECTORY ,DEPTH ));
You can’t perform that action at this time.
0 commit comments