About Question enthuware.ocpjp.v7.2.1217 :

Moderator: admin

Post Reply
SepticInsect
Posts: 20
Joined: Tue Nov 04, 2014 1:13 am
Contact:

About Question enthuware.ocpjp.v7.2.1217 :

Post by SepticInsect » Sun Dec 14, 2014 6:37 am

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.

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Dec 14, 2014 8:08 am

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.
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post by romsky » Sat Jul 04, 2015 8:39 am

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).

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Jul 04, 2015 9:53 am

Did you try it out?
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post by romsky » Sat Jul 04, 2015 11:14 am

admin wrote:Did you try it out?
Yes, it does not find neiher "a." on "a," , strange...

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Jul 04, 2015 1:05 pm

Why do you think it should find a. or a,? It should match a dot something, not just a dot.
If you like our products and services, please help us by posting your review here.

romsky
Posts: 39
Joined: Thu Jan 29, 2015 4:49 pm
Contact:

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

Post by romsky » Sat Jul 04, 2015 2:25 pm

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.

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Jul 04, 2015 11:07 pm

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.
If you like our products and services, please help us by posting your review here.

Chen@ukr.net
Posts: 9
Joined: Sat Feb 27, 2016 1:17 pm
Contact:

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

Post by Chen@ukr.net » Sun Apr 17, 2016 10:45 am

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}

admin
Site Admin
Posts: 8702
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sun Apr 17, 2016 10:52 am

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest