如何防止java FileTreeWalker过早失败

时间:2017-09-13 17:31:47

标签: java nio2

我正在尝试将文件树移动到计数文件中。但是即使我在我的CountFiles类中处理异常,对walkFileTree()的调用也过早地失败了,而FileTreeWalker类则阻止它对剩余文件进行计数。

我该如何避免这种情况?

countFiles = new CountFiles(BaseFolderGuesser.FILE_SUFFIX_SEARCH_PATTERN);
Files.walkFileTree(path, countFiles);
totalCount+=countFiles.getFileCount();

public static class CountFiles
            extends SimpleFileVisitor<Path>
    {
        private int fileCount = 0;
        private Pattern pattern;

        public CountFiles(String regex)
        {
            pattern = Pattern.compile(regex);
        }

        /**
         * SONGKONG-294  Ignore the /proc virtual fs on linux
         *
         * @param dir
         * @param attrs
         * @return
         * @throws IOException
         */
         /*
         * Ignore some dirs
         * @param dir
         * @param attrs
         * @return
         * @throws IOException
         */
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                throws IOException
        {

            if (dir.toString().equals("/proc")) {
                MainWindow.logger.log(Level.SEVERE,"Ignoring /proc");
                return FileVisitResult.SKIP_SUBTREE;
            }
            else if (RecycleBinFolderNames.isMatch(dir.toFile().getName()))
            {
                MainWindow.logger.log(Level.SEVERE,"Ignoring "+dir.toString());
                return FileVisitResult.SKIP_SUBTREE;
            }
            return super.preVisitDirectory(dir, attrs);
        }

        /**
         * Find Music file
         *
         * @param file
         * @param attr
         * @return
         */
        @Override
        public FileVisitResult visitFile(Path file,
                                         BasicFileAttributes attr)
        {
            Path name = file.getFileName();
            if (name != null && pattern.matcher(name.toString().toLowerCase(Locale.UK)).matches())
            {
                fileCount++;
            }
            return FileVisitResult.CONTINUE;
        }

        /**
         * http://stackoverflow.com/questions/14436032/why-is-java-7-files-walkfiletree-throwing-exception-on-encountering-a-tar-file-o/14446993#14446993
         * SONGKONG-294:Ignore exceptions if file is not readable
         *
         * @param file
         * @param exc
         * @return
         * @throws IOException
         */
        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {

            if (file.toString().endsWith(".tar")) {
                //We dont log to reports as this is a bug in Java that we are handling not a problem in SongKong
                MainWindow.logger.log(Level.SEVERE, exc.getMessage());
                return FileVisitResult.CONTINUE;
            }

            try
            {
                FileVisitResult result = super.visitFileFailed(file, exc);
                return result;
            }
            catch(AccessDeniedException ade)
            {
                MainWindow.logger.warning("Unable to count files in:"+file);
                return FileVisitResult.CONTINUE;
            }
        }

        /**
         * SONGKONG-294:Ignore exception if folder is not readable
         *
         * @param dir
         * @param exc
         * @return
         * @throws IOException
         */
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                throws IOException
        {
            try
            {
                FileVisitResult result = super.postVisitDirectory(dir, exc);
                return result;
            }
            catch(AccessDeniedException ade)
            {
                MainWindow.logger.warning("Unable to count files in dir:"+dir);
                return FileVisitResult.CONTINUE;
            }
        }


        public int getFileCount()
        {
            return fileCount;
        }
    }

给出错误

  13/09/2017 11.25.11:EDT:com.jthink.songkong.fileloader.CountFilesinFolder:handleException:SEVERE: Unable to count files:/Volumes/PlexMedia/Music/White Stripes, The - De Stijl/1BN0PB~Y
    java.nio.file.NoSuchFileException: /Volumes/PlexMedia/Music/White Stripes, The - De Stijl/1BN0PB~Y
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
    at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:144)
    at java.nio.file.Files.readAttributes(Files.java:1737)
    at java.nio.file.FileTreeWalker.getAttributes(FileTreeWalker.java:219)
    at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:276)
    at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
    at java.nio.file.Files.walkFileTree(Files.java:2706)
    at java.nio.file.Files.walkFileTree(Files.java:2742)
    at com.jthink.songkong.fileloader.CountFilesinFolder.call(CountFilesinFolder.java:174)
    at com.jthink.songkong.fileloader.CountFilesinFolder.call(CountFilesinFolder.java:26)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:3)

您的类扩展了SimpleFileVisitor,并且在您重写的visitFileFailed方法中写道:

        try
        {
            FileVisitResult result = super.visitFileFailed(file, exc);
            return result;
        }
        catch(AccessDeniedException ade)
        {
            MainWindow.logger.warning("Unable to count files in:"+file);
            return FileVisitResult.CONTINUE;
        }

你打电话给你的超级类'visitFileFailed:

/**
 * Invoked for a file that could not be visited.
 *
 * <p> Unless overridden, this method re-throws the I/O exception that prevented
 * the file from being visited.
 */
@Override
public FileVisitResult visitFileFailed(T file, IOException exc)
    throws IOException
{
    Objects.requireNonNull(file);
    throw exc;
}

重新抛出之前处理的异常。但是你得到了例外。之后你只捕获AccessDeniedException,但我可以看到你得到NoSuchFileException。

也许您可以考虑捕获更广泛的异常或者根本不调用super.visitFileFailed,因为它什么都不做。