gpt4 book ai didi

java-8 - 忽略异常并继续和计数目录中的文件

转载 作者:行者123 更新时间:2023-12-04 04:34:28 25 4
gpt4 key购买 nike

以下代码段计算目录中的文件数:

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

如果引发异常,则上面的代码无法获取文件数。

问题是:如何忽略该异常并继续计算文件数。

在Java 7中:我使用了 Files.walkFileTree(path, utils)和以下类:
 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;
}

}

编辑:这是异常的堆栈跟踪:
  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

最佳答案

到目前为止,似乎Files.walk在流中仍然不能很好地播放。相反,您可能想改用Files.newDirectoryStream。这里的一些代码片段可能会有所帮助。

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

关于java-8 - 忽略异常并继续和计数目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com