我正在尝试通过文件树来计数文件。但是,即使我在CountFiles类中处理异常,但在FileTreeWalker类中,对walkFileTree()的调用也过早失败,从而阻止了对剩余文件的计数。
如何避免这种情况?
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)
最佳答案
您的类扩展了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,因为它什么都不做。
关于java - 如何防止Java FileTreeWalker过早失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46203630/