2015-01-03 13 views
5

Dal libro di "Kathy Sierra Bert Bates" per l'esame OCP ho trovato il seguente codiceGlob modello utilizzando getPathMatcher

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:**"); 

    } 
} 

uscita:

false 
true 
false 
true 

non riesco a capire l'output in modo chiaro. Qualcuno mi spiegherebbe. fammi sapere il mio esempio che cosa sta attraversando i confini delle directory. Grazie Rocciose

+2

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

+0

@BatScream il codice funziona per me come è in Windows OS, e ottengo lo stesso risultato indicato dall'OP – Adil

risposta

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. 

    } 
} 

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

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

perché il vostro percorso contiene / che descrivono una gerarchia di directory, *.java corrisponde a qualsiasi nome di file con .java estensione

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

perchè ** corrisponde a qualsiasi stringa, incluso sotto-percorso (come /com/java/ in voi e xample)

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

come accennato nella prima, perché avete separatore di percorso /

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

come accennato nel secondo, perché ** corrisponde a qualsiasi stringa tra cui /