Java OCA OCP Practice Question 2581

Question

Consider the following program and predict the output (the following files exist in the given path File09.java, File0.java, FileVisitor1.java, FileVisitor1.class):

class MyFileFindVisitor extends SimpleFileVisitor<Path> {
    private PathMatcher matcher;
    public MyFileFindVisitor(String pattern) {
            matcher =/*from   w  w w  . j a v  a  2 s.c o m*/
                    FileSystems.getDefault().getPathMatcher(pattern);
    }
    public FileVisitResult visitFile
            (Path path, BasicFileAttributes fileAttributes){
            find(path);
            return FileVisitResult.CONTINUE;
    }
    private void find(Path path) {
            Path name = path.getFileName();
            if(matcher.matches(name))
                    System.out.println
                           ("Matching file:" + path.getFileName());
    }
    public FileVisitResult preVisitDirectory
            (Path path, BasicFileAttributes fileAttributes){
            find(path);
            return FileVisitResult.CONTINUE;
    }
}

class FileTreeWalkFind {
    public static void main(String[] args) {
            Path startPath = Paths.get("d:\\workspace\\test\\src");
            String pattern = "glob:File[0-9]+.java";
            try {
                    Files.walkFileTree
                    (startPath, new MyFileFindVisitor(pattern));
                    System.out.println("File search completed!");
            } catch (IOException e) {
                    e.printStackTrace();
            }
    }
}
a)      File09.java//from   www . j  a  v  a  2s  . c  o  m
        File0.java
        FileVisitor1.java
        File search completed!

b)       File09.java
         File0.java
         File search completed!

c)      File0.java
        File search completed!

d)     File search completed!


d)

Note

Well, glob does not support "+"; hence, the specified glob expression does not find any file matching with the expression.




PreviousNext

Related