About Question enthuware.ocpjp.v7.2.1217 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

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 »

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: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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 »

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

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

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

Post by admin »

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 »

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: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
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 »

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: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests