Why is Java 7 Files.walkFileTree throwing exception on encountering a tar file on remote drive

后端 未结 1 580
灰色年华
灰色年华 2020-12-17 10:26

Im using Files.WalkFileTree() to navigate folder and counting audio files, but there is a problem when it encounters a tar file, it seems to be treating it as a

相关标签:
1条回答
  • 2020-12-17 11:09

    Workaround for the problem at hand:

    But implement a visitFileFailed and you should be ok.

    public class MyFileVisitor extends SimpleFileVisitor<Path> {
        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            if (file.toString().endsWith(".tar")) {
                return FileVisitResult.CONTINUE;
            }
            return super.visitFileFailed(file, exc);
        }
    }
    

    Update: If we look closer we can see that walkFileTree uses the Files.readAttributes which turns to the current provider in play: WindowsFileSystemProvider.readAttributes to determine if a path is a directory.

    As someone mentioned in the comments I also dont think the fault is in the Java-implementation but the OS-native-call that returns the wrong attribute

    If you wanted to do a workaround for this, one option would be to implement your own FileSystem that wraps the WindowsFileSystem implementation transparently, except readAttributes returns .tar-paths as file instead of dir.

    0 讨论(0)
提交回复
热议问题