[BAEL-5554] Find files that match a wildcard string in Java by @opokharel (#12190)
* BAEL-5554 by @opokharel * deletedToMoveToSrcFolder * movedToSrcFolder * redoingForJenkins * newPR for [BAEL-5554] Find files that match a wildcard string in Java by @opokharel * @opokharel * [BAEL-5554] @opokharel * [BAEL-5554] Find files that match a wildcard string in Java by @opokharel * Update SearchFileByWildcardTest.java * Update SearchFileByWildcard.java * Update SearchFileByWildcard.java * Create SearchFileByWildcardUnitTest.java * Delete SearchFileByWildcardTest.java * [BAEL-5554] Find files that match a wildcard string in Java by @opokharel * Update SearchFileByWildcardUnitTest.java * Update SearchFileByWildcardUnitTest.java * [BAEL-5554] UnitTestFiles by @opokharel * Update core-java-modules/core-java-nio-2/src/test/java/com/baeldung/searchfilesbywildcards/SearchFileByWildcardUnitTest.java Co-authored-by: KevinGilmore <kpg102@gmail.com> * updated curly braces and assertions Co-authored-by: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
parent
75a751df3a
commit
f8ce248236
|
@ -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<String> matchesList = new ArrayList<String>();
|
||||
public List<String> searchWithWc(Path rootDir, String pattern) throws IOException {
|
||||
matchesList.clear();
|
||||
FileVisitor<Path> matcherVisitor = new SimpleFileVisitor<Path>() {
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -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<String> 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<String> actual = sfbw.searchWithWc(Paths.get("src/test/resources/sfbw"), "glob:????.{csv}");
|
||||
|
||||
assertEquals(new HashSet<>(Arrays.asList("five.csv")), new HashSet<>(actual));
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue