10

The following snippet of code counts the number of files in a directory:

      Path path = ...;
       ....
      long count = 0;
        try {
           count  = Files.walk(path).parallel().filter(p -> !Files.isDirectory(p)).count();

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }

The code above fails to get the number of files, if an exception is thrown.

The question is: How do I ignore the exception and continue counting the number of files.

In Java 7: I have used Files.walkFileTree(path, utils), with the following class:

 public class Utils extends SimpleFileVisitor<Path> {

    private long count;

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (attrs.isRegularFile()) {
            count++;

        }
        return CONTINUE;
    }

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

    public long countFilesInDirectoryJava7() {
        return count;
    }

}

Edit: Here is the stack trace of the exception:

  Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: E:\8431c36f5b6a3d7169de9cc70a\1025
    at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:88)
    at java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:104)
    at java.util.Spliterators$IteratorSpliterator.trySplit(Spliterators.java:1784)
    at java.util.stream.AbstractTask.compute(AbstractTask.java:297)
    at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinTask.doInvoke(ForkJoinTask.java:401)
    at java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:734)
    at java.util.stream.ReduceOps$ReduceOp.evaluateParallel(ReduceOps.java:714)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
    at java.util.stream.LongPipeline.reduce(LongPipeline.java:438)
    at java.util.stream.LongPipeline.sum(LongPipeline.java:396)
    at Utils.countFilesInDirectoryJava8(Utils.java:47)
    at TestPath.main(TestPath.java:27)
Caused by: java.nio.file.AccessDeniedException: E:\8431c36f5b6a3d7169de9cc70a\1025
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
    at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:518)
    at java.nio.file.Files.newDirectoryStream(Files.java:457)
    at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
    at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
    at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:84)
    ... 13 more
Java Result: 1
Kachna
  • 2,921
  • 2
  • 20
  • 34
  • What's the stack trace of the exception? – JB Nizet Sep 05 '15 at 12:31
  • @JBNizet I updated the question with the stack trace of the exception. – Kachna Sep 05 '15 at 12:54
  • 3
    Given the exception, I don't think you can do anything about it. Keep using the SimpleFileVisitor. – JB Nizet Sep 05 '15 at 12:59
  • 1
    I'm worry it is impossible indeed. You could easily modify the reimplement `Files.walk()` to ignore the exception but, unfortunately, they made `FileTreeIterator` and related classes package private, so you would end up rewriting the whole *walk* algorithm yourself. – Mifeet Sep 06 '15 at 20:40
  • 1
    By the way, don’t use `.mapToLong(j -> 1).sum()` when you want to count. Just use ……… `count()` – Holger Sep 07 '15 at 10:43
  • @Holger, Thanks. nice suggestion. – Kachna Sep 08 '15 at 13:23

1 Answers1

2

So far, it seems Files.walk still does not play well with streams. Instead, you might want to use Files.newDirectoryStream instead. Here's some code snippet that might help.

    static class FN<T extends Path> implements Function<T, Stream<T>> {
    @Override
    public Stream<T> apply(T p) {
        if (!Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS)) {
            return Stream.of(p);
        } else {
            try {
                return toStream(Files.newDirectoryStream(p)).flatMap(q -> apply((T) q));
            } catch (IOException ex) {
                return Stream.empty();
            }
        }
    }
}

public static void main(String[] args) throws IOException {
    Path path = Paths.get("some path");
    long count = toStream(Files.newDirectoryStream(path))
            .parallel()
            .flatMap(new FN<>()::apply)
            .count();             

    System.out.println("count: " + count);
}

static <T> Stream<T> toStream(Iterable<T> iterable) {
    return StreamSupport.stream(iterable.spliterator(), false);
}
rrufai
  • 1,475
  • 14
  • 26