2015-01-03 11 views
5

Aus dem Buch von "Kathy Sierra Bert Bates" für OCP Prüfung fand ich den folgenden CodeGlob Muster getPathMatcher mit

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"); 
     matches(path, "glob:**/*.java"); 
     matches(path, "glob:*"); 
     matches(path, "glob:**"); 

    } 
} 

Ausgang:

false 
true 
false 
true 

ich den Ausgang nicht eindeutig verstehen kann. Würde mir jemand erklären. lassen Sie mich mein Beispiel wissen, was Verzeichnisgrenzen überschreitet. Dank Rocky

+2

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

+0

@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

Antwort

1
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

2
matches(path, "glob:*.java"); // => flase 

weil Ihr Pfad enthält /, die eine Verzeichnishierarchie beschreiben, *.java entspricht einem beliebigen Dateinamen mit .java Erweiterung

matches(path, "glob:**/*.java"); // => true 

weil ** Matches eine beliebige Zeichenfolge, einschließlich Unterpfad (wie /com/java/ in Ihnen e xample)

matches(path, "glob:*"); // => false 

wie in der ersten erwähnt, weil Sie Pfadseparator haben /

matches(path, "glob:**"); // => true 

wie in der zweiten erwähnt, weil ** Matches eine beliebige Zeichenfolge /