Page 1 of 1

About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sun Dec 14, 2014 6:37 am
by SepticInsect

Code: Select all

public class FileVisitorTest extends SimpleFileVisitor<Path> {
    private final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.c");
    private static int count;

    public int getCount() {
        return count;
    }

    public FileVisitResult visitFile(Path path, BasicFileAttributes attributes) {
        Path name = path.getFileName();
        System.out.println(name);
        if (name != null && matcher.matches(name)) {
            count++;
        }
        return FileVisitResult.CONTINUE;
    }

    public static void main(String[] args) throws IOException {
        FileVisitorTest fileVisitorTest = new FileVisitorTest();
        Files.walkFileTree(Paths.get("C:\\Users\\<user>\\workspace\\ocp\\resources"), fileVisitorTest);
        System.out.println(fileVisitorTest.getCount());
    }
}
When do I need to use ** in the PathMatcher? In the example above the FileVisitor will go through all subdirectories.

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sun Dec 14, 2014 8:08 am
by admin
Because * doesn't cross subdirectories. It only matches files in the current directory. To cross a directory boundary, you need **.
You might want to go through a tutorial on glob pattern before attempting questions on this topic.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sat Jul 04, 2015 8:39 am
by romsky
Look at the explanation for third variant:
"Square brackets [] are used to specify multiple options for a single character. Therefore, the above pattern will match a.h, a.t, a.m, a., a.x and a.l but not a.htm or a.xml."

I think instead of "a." must be "a," (a comma, rather then a dot).

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sat Jul 04, 2015 9:53 am
by admin
Did you try it out?

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sat Jul 04, 2015 11:14 am
by romsky
admin wrote:Did you try it out?
Yes, it does not find neiher "a." on "a," , strange...

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sat Jul 04, 2015 1:05 pm
by admin
Why do you think it should find a. or a,? It should match a dot something, not just a dot.

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sat Jul 04, 2015 2:25 pm
by romsky
admin wrote:Why do you think it should find a. or a,? It should match a dot something, not just a dot.
Because in the explanation for third variant with pattern "glob:**.[htm,xml]",
there is "a." as possible match.

That is wrong: "glob:**.[htm,xml]" does NOT match "a." .
I was wrong too: it does not match "a," too.

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sat Jul 04, 2015 11:07 pm
by admin
Ok, I understand the problem now. The explanation is correct but it is missing a comma after a,. I.e. it should say, "the above pattern will match a.h, a.t, a.m, a.,, a.x and a.l but not a.htm or a.xml."

I couldn't see earlier that that comma was missing. So the bottom line is it will not match a, (a comma) but it will match a., (a dot comma) because of the reason mentioned in the explanation.

-Paul.

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sun Apr 17, 2016 10:45 am
by Chen@ukr.net
The value of the syntax component is compared without regard to case.
I try:

Code: Select all

PathMatcher pm = FileSystems.getDefault().getPathMatcher("GLOB:**.{htm*,xml}");
and get
Exception in thread "main" java.lang.UnsupportedOperationException: Syntax 'GLOB' not recognized
at sun.nio.fs.WindowsFileSystem.getPathMatcher(WindowsFileSystem.java:299)
at javaapplication219.JavaApplication219.main(JavaApplication219.java:21)
C:\Users\Media\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
what you mean "without regard to case"?
or you mean this part:**.{htm*,xml}

Re: About Question enthuware.ocpjp.v7.2.1217 :

Posted: Sun Apr 17, 2016 10:52 am
by admin
glob: is part of the syntax of glob pattern. It has to be lower case. It is the case of the actual pattern that is being referred to in the explanation i.e. htm or HTM.

HTH,
Paul.