import java.nio.file.*; import java.io.IOException; public class GlobExample { public static void main(String[] args) { PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.{txt}"); try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get("/path/to/directory"))) { for (Path path : stream) { if (matcher.matches(path.getFileName())) { System.out.println(path); } } } catch (IOException e) { e.printStackTrace(); } } } <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency> </dependencies>


上一篇:
下一篇:
切换中文