Java 判断后缀是否匹配规则

Java About 448 words

PathMatcher

使用JDK7提供的API。之前一般使用endsWith判断以指定格式结尾。

public static void main(String[] args) {
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.{gitignore,java,class,md,html}");

    Path filename = Paths.get(".gitignore");
    if (matcher.matches(filename)) {
        System.out.println(filename);
    } else {
        System.out.println("not match");
    }
}

参考

https://docs.oracle.com/javase/tutorial/essential/io/find.html

Views: 2,345 · Posted: 2020-05-26

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh