ignore exception and continue & counting files in

2019-04-22 18:55发布

问题:

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

回答1:

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);
}