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.
}
}
In dem obigen Programm, wenn Sie Übereinstimmungen mit dem Pfad als "/com/java/One.java"
fordern und regulären Ausdruck glob für die Suche oder den Pfad der Funktion entsprechen, werden die Werte nehmen und die Operation durchführen und das Rück wahr oder falsch . Ausgang:
true
false
true
true
Wenn Sie die Windows-Plattform verwenden, dann müssen Sie das Programm wie folgt ändern.
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:**");
}
}
Weitere Details Click here
einschließlich weiß ich nicht Holen Sie sich die Ausgabe, die Sie für den Eingabepfad angegeben haben. Aber ich bekomme Ihre Ausgabe, wenn ich nur den Dateinamen - 'Paths.get (" One.java ")' übergeben. Unter Windows, NTFS-Dateisystem. Auf welchem Betriebssystem und in welchem Dateisystem haben Sie diesen Code getestet? – BatScream
@BatScream der Code funktioniert für mich, wie es in Windows OS ist, und ich bekomme das gleiche Ergebnis wie vom OP erwähnt – Adil