OCPJP708.5 Word Scramble
|
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.
Normal Size Small Size show me how
Normal Size Small Size show me how
| [8.5.1] Objective | Find a file with PathMatcher interface. |
| [8.5.2] What is a glob? | PathMatchers use a new type that you probably haven't seen before called a glob. Globs are not regular expressions, although they might look similar at first. |
| [8.5.3]In the world of globs, one asterisk means "match any character except for a directory boundary." Two asterisks means "match any character, including a directory boundary." | Path path = Paths.get("/com/java/One.java"); matches(path, "glob:*.java"); // false matches(path, "glob:**/*.java"); // true matches(path, "glob:*"); // false matches(path, "glob:**"); // true |
| [8.5.4] Remember that we are using a file system–specific PathMatcher. This means slashes and backslashes can be treated differently, depending on what operating system you happen to be running. | The previous example does print the same output on both Windows and UNIX because it uses forward slashes. |
| [8.5.5] Unix vs Windows | Path path = Paths.get("com\\java\\One.java"); Now Windows still prints: false true false true However, UNIX prints: true false true true |
| [8.5.6] Preferred | Why? Because UNIX doesn't see the backslash as a directory boundary. The lesson here is to use / instead of \\ so your code behaves more predictably across operating systems. |
| [8.5.7]A question mark matches any character. A character could be a letter or a number or anything else | Path path1 = Paths.get("One.java"); Path path2 = Paths.get("One.ja^a"); matches(path1, "glob:*.????"); // true matches(path1, "glob:*.???"); // false matches(path2, "glob:*.????"); // true matches(path2, "glob:*.???"); // false |
| [8.5.8]Boilerplate code | public void matches(Path path, String glob) { PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob); System.out.println(matcher.matches(path)); } |
| [8.5.9] More examples | Path path1 = Paths.get("Bert-book"); Path path2 = Paths.get("Kathy-horse"); matches(path1, "glob:{Bert*,Kathy*}"); // true matches(path2, "glob:{Bert,Kathy}*"); // true matches(path1, "glob:{Bert,Kathy}"); // false |
| [8.5.10] More on glob1 (Java won't let you type a single backslash, so you have to escape the backslash itself with another backslash.) | ■ [0-9] One single digit. Can also be read as any one character from 0 to 9. ■ \\* The literal character asterisk rather than the asterisk that means to match anything. A single backslash before * escapes it. |
| [8.5.11] More on glob2 | ■ {A*,b} Either a capital A followed by anything or the single character b. ■ /**/ One or more directories with any name. ■ 1 The single character 1. |
Created by:
MVK2013
Popular Computers sets