public class FileTest {
public static void matches(Path path, String glob){
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
System.out.println(matcher.matches(path));
}
public static void main(String[] args) throws IOException {
Path path = Paths.get("/com/java/One.java");
matches(path, "glob:*.java"); // regular expression that matches any file path that end with .java so it will return the value as true
matches(path, "glob:**/*.java"); // regular expression ** characters matches zero or more characters crossing directory boundaries so it will match complete path but if you put /* it will search for a path like this /com/java//one.java soe here it will not match the path and will return value as false.
matches(path, "glob:*"); // this will match any path and return value as true.
matches(path, "glob:**"); // this will complete path crossing directory so it will return you value as true.
}
}
Nel programma precedente quando si sta chiamando le partite con il percorso come "/com/java/One.java"
e Glob espressione regolare per la ricerca o corrispondente al percorso della funzione prenderà i valori ed eseguire l'operazione e restituire true o false . uscita:
true
false
true
true
Se si utilizza la piattaforma Windows allora avete bisogno di modificare il programma come segue.
public class match {
public static void matches(Path path, String glob){
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
System.out.println(matcher.matches(path));
}
public static void main(String[] args) throws IOException {
Path path = Paths.get("\\com\\java\\One.java");
matches(path, "glob:*.java");
matches(path, "glob:**\\*.java");
matches(path, "glob:*");
matches(path, "glob:**");
}
}
Per maggiori dettagli Click here
non lo faccio ottieni l'output che hai fornito per il percorso di input. Ma ottengo il tuo output quando ho appena passato il nome del file - 'Paths.get (" One.java ")'. Su Windows, file system NTFS. Quale sistema operativo e quale file system hai testato su questo codice? – BatScream
@BatScream il codice funziona per me come è in Windows OS, e ottengo lo stesso risultato indicato dall'OP – Adil