diff --git a/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java new file mode 100644 index 0000000000..2deaa60ec6 --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/main/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcard.java @@ -0,0 +1,37 @@ +package com.baeldung.searchfilesbywildcards; + +import java.io.IOException; + +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.PathMatcher; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +import java.util.ArrayList; +import java.util.List; + +public class SearchFileByWildcard { + public static List matchesList = new ArrayList(); + public List searchWithWc(Path rootDir, String pattern) throws IOException { + matchesList.clear(); + FileVisitor matcherVisitor = new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attribs) throws IOException { + FileSystem fs = FileSystems.getDefault(); + PathMatcher matcher = fs.getPathMatcher(pattern); + Path name = file.getFileName(); + if (matcher.matches(name)) { + matchesList.add(name.toString()); + } + return FileVisitResult.CONTINUE; + } + }; + Files.walkFileTree(rootDir, matcherVisitor); + return matchesList; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java new file mode 100644 index 0000000000..71cee036ec --- /dev/null +++ b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.searchfilesbywildcards; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; + +import java.nio.file.Paths; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class SearchFileByWildcardUnitTest { + @Test + public void whenFourFilenameMatch_thenListOfFour() throws IOException { + SearchFileByWildcard sfbw = new SearchFileByWildcard(); + List actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:*.{txt,docx}"); + + assertEquals(new HashSet<>(Arrays.asList("six.txt", "three.txt", "two.docx", "one.txt")), new HashSet<>(actual)); + } + @Test + public void whenOneFilenameMatch_thenListOfOne() throws IOException { + SearchFileByWildcard sfbw = new SearchFileByWildcard(); + List actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:????.{csv}"); + + assertEquals(new HashSet<>(Arrays.asList("five.csv")), new HashSet<>(actual)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/five.csv b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/five.csv new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/four.xlsx b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/four.xlsx new file mode 100644 index 0000000000..1091dafe75 Binary files /dev/null and b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/four.xlsx differ diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/one.txt b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/one.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/test2/six.txt b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/test2/six.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/three.txt b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/three.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/two.docx b/core-java-modules/core-java-nio-2/src/test/resources/sfbw/test/two.docx new file mode 100644 index 0000000000..e69de29bb2