PathMather使用方法

参考文章

========链接========

java.nio.file.PathMatcher is an interface introduced in JDK 7. It has a method matches() that matches the pattern with given path. PathMatcher is instantiated by getPathMatcher() which is the method of java.nio.file.FileSystem. Find the syntax below. 
PathMatcher getPathMatcher(String synatxNpattern) 
We need to pass string in given format. 
syntax:pattern 
Here syntax can be "glob" and "regex". 
While using "glob" as a syntax, we can use below patterns. 

*.java when given path is java , we will get true by PathMatcher.matches(path). 
*.* if file contains a dot, pattern will be matched. 
*.{java,txt} If file is either java or txt, path will be matched. 
abc.? matches a file which start with abc and it has extension with only single character. 


一、概述

PathMatcher是一个函数式接口,该接口可以自己进行实现,也可以用FileSystem中提供的PathMatcher,

1、自己实现PathMatcher 

可以定义实现类、匿名类或者lamda表达式(PathMatcher是@FunctionalInterface)

2.使用FileSystem中提供的PathMatcher  已经定义好了一种匹配的原则

由FileSystem 中的非静态方法实例化,作用是判断一个路径是否符合匹配原则,该原则由getPathMatcher参数synatxNpattern指定:函数声明 PathMatcher getPathMatcher(String synatxNpattern) 

@parm synatxNpattern  有两种选择:  glob  和 regex

2.1. glob  用特定的字符进行模式匹配

It matches zero , one or more than one characters. While matching, it will not cross directories boundaries.  

一个或者多个字符,不包括斜线
** It does the same as * but it crosses the directory boundaries. 

一个或者多个字符,可包括斜线
It matches only one character for the given name. 

一个字符
It helps to avoid characters to be interpreted as special characters. 

转义字符
[] In a set of characters, only single character is matched. If (-) hyphen is used then, it matches a range of characters. Example: [efg] matches "e","f" or "g" . [a-d] matches a range from a to d. 

范围表达式  [efg] 符合efg的    [a-d] a到d范围的
{} It helps to matches the group of sub patterns. 
2.22. regex  java.util.regex.Pattern中定义的模式
In case of "regex", pattern is regular expression defined by java.util.regex.Pattern. Read java.util.regex.Pattern to get "regex" more.


二、实例:

1.自定义实现类

public class MyPathMatcher implements PathMatcher{
	@Override
	public boolean matches(Path path) {
		return path.toString().contains("abc");
	}
}

2.匿名内部类

public class Main {

	public static void main(String[] args) {
		PathMatcher pm =new PathMatcher(){
			@Override
			public boolean matches(Path path) {
				return path.toString().contains("ini");
			}
		};
		
		Path path = Paths.get("d:/wpeinit");
		System.out.println(pm.matches(path));
	}

}
output
true

3.lamda表达式

public class Main {

	public static void main(String[] args) {
		//()表示语句,省略return
		PathMatcher pm = (Path path) -> (path.toString().contains("ini"));
		//形参类型省略
		PathMatcher pm1 = (path) -> (path.toString().contains("ini"));
		//使用return时 ,必须使用{}  代表语句块
		PathMatcher pm2 = (path) -> {return path.toString().contains("ini");};
		
		System.out.println(pm.matches(Paths.get("d:/wpeinit")));
		System.out.println(pm1.matches(Paths.get("d:/wpeinit")));
		System.out.println(pm2.matches(Paths.get("d:/wpeinit")));
	}

}
output:
true
true
true
4.使用FileSystem提供的PathMatcher

public class Main {
	public static void main(String[] args) {
		FileSystem fs = FileSystems.getDefault();
		//匹配模式是:** 任意多个字符(包括斜线)ini [stf]s、t、f三个字符中任意一个
		PathMatcher pm = fs.getPathMatcher("glob:**ini[stf]");
		//注意此处为Paths.get()
		System.out.println(pm.matches(Paths.get("D:/wpeinit")));
	}
}
output: true




  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值