【问题标题】:Can Java Files.walk continue executing after encountering an exception errorJava Files.walk遇到异常错误后能否继续执行
【发布时间】:2021-07-03 21:04:58
【问题描述】:

这是我第一次在 Java 中使用 Files.walk,我很喜欢它,尤其是 Path 类。它功能强大且用途广泛。

然而,我不喜欢的是,在遍历文件夹时,如果迭代器遇到障碍......就像它遇到了用户无权查看的文件夹或它可能遇到的任何错误,在它抛出异常之后,它不会继续,也不会产生一个可以使用的路径列表。

这是我如何使用该方法的示例:

try {
    Stream<Path> paths    = Files.walk(rootPath);
    List<Path>   pathList = paths.collect(Collectors.toList());
    for (Path path : pathList) {
        //Processing code here
    }
 }
catch (FileSystemException a) {System.err.format("FileSystemException: %s%n", a);System.out.println(a.getStackTrace());}
catch (UncheckedIOException b) {System.err.format("UncheckedIOException: %s%n", b);System.out.println(b.getCause().getLocalizedMessage());}
catch (IOException c) { System.err.format("IOException: %s%n", c);System.out.println(c.getStackTrace());}

现在,如果 Files.walk 方法在尝试遍历文件夹时遇到某种错误,它会抛出异常,然后代码当然会在捕获异常后继续。

我想要它做的是忽略它遇到的任何问题,并继续迭代它可以不停止的任何文件夹。

这可能吗?

【问题讨论】:

    标签: java java-io


    【解决方案1】:

    您可以使用Files#walkFileTree() 方法并实现您自己的访问者对象,该对象将忽略异常:

    public class Test
    {
        public static void main(String[] args) throws Exception
        {
            
            Path rootPath = Path.of("e:\\walkMe");
            Files.walkFileTree(rootPath,new MyVisitor());
        
        }
    }
    class MyVisitor implements FileVisitor<Path> {
    
        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
            System.out.println(dir);
            return FileVisitResult.CONTINUE;
        }
    
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            System.out.println(file);
            return FileVisitResult.CONTINUE;
        }
    
        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) {
            
            System.out.println("error on: " + file + " " + exc.getClass());
            return FileVisitResult.CONTINUE;
        }
    
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) {    
            return FileVisitResult.CONTINUE;
        }
    }
    

    输出:

    e:\walkMe
    e:\walkMe\ab
    e:\walkMe\ab\ab.txt
    error on: e:\walkMe\cd class java.nio.file.AccessDeniedException
    e:\walkMe\ef
    e:\walkMe\ef\ef.txt
    

    在这里,应用程序无法浏览文件夹 cd,但它会继续遍历其余的文件夹和文件。

    【讨论】:

    • 谢谢你,我最终深入研究了如何使用 FileVisitor 界面,并能够为我正在做的事情创建一个稳定的解决方案。
    【解决方案2】:

    我们仍然可以像这样使用Files#walk

    Path rootPath = Path.of("e:\\walkMe");
    Iterator<Path> itr = Files.walk(rootPath).iterator();
    while(true) {
        try {
            if(itr.hasNext()) {
                System.out.println(itr.next());
            } else {
                break;
            }
        }catch(Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }
    

    输出:

    e:\walkMe
    e:\walkMe\ab
    e:\walkMe\ab\ab.txt
    java.nio.file.AccessDeniedException: e:\walkMe\cd
    e:\walkMe\ef
    e:\walkMe\ef\ef.txt
    

    【讨论】:

    • 我喜欢这个! treewalk 的东西似乎更健壮且功能丰富,但这很好。
    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    相关资源
    最近更新 更多