1

This is my Java Class:

public class PrintFiles extends java.nio.file.SimpleFileVisitor<Path> {

@Override
public FileVisitResult visitFile(Path file,
                               BasicFileAttributes attr) {
    if (attr.isSymbolicLink()) {
        System.out.format("Symbolic link: %s ", file);
    } else if (attr.isRegularFile()) {
        System.out.format("Regular file: %s ", file);
    } else {
        System.out.format("Other: %s ", file);
    }
    System.out.println("(" + attr.size() + "bytes)");
    return CONTINUE;
 }

 // Print each directory visited.
 @Override
 public FileVisitResult postVisitDirectory(Path dir,
                                      IOException exc) {
    System.out.format("Directory: %s%n", dir);
    return CONTINUE;
}


@Override
public FileVisitResult visitFileFailed(Path file,
                                   IOException exc) {
    System.err.println(exc);
    return CONTINUE;

}

public static void main(String args[])
    {

    java.nio.file.Path startingDir = Paths.get("D:\\");
    PrintFiles pf = new PrintFiles();
    Files.walkFileTree(startingDir, pf);
    }
    }

This line:

 Files.walkFileTree(startingDir, pf);

throws error: PrintFiles cannot be converted to <? super Path> which is acceptable but When I change the above line to:

Files.walkFileTree(startingDir, new java.nio.file.SimpleFileVisitor<Path> ());

it still shows the error SimpleFileVisitor() has protected access in SimpleFileVisitor.So how am i suppose to run walkFileTree method.I am trying to run the example from here.I have also seen examples on SO using above code but don't know why I am facing problem.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Fresher
  • 894
  • 1
  • 7
  • 27
  • 1
    I copied the code you posted and, other then an uncaught IOException, it runs fine. Double check the generic type `Path`, it should be `java.nio.file.Path`. Any (test) class called `Path` in your project/workspace might be confusing the compiler. – vanOekel Jul 19 '14 at 09:27
  • @vanOekel Yes it worked fine.Thanks actually i had a class name `Path` in my package that was causing this problem. – Fresher Jul 19 '14 at 11:29

0 Answers0