gpt4 book ai didi

java - 如何防止 java FileTreeWalker 过早失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:21 24 4
gpt4 key购买 nike

我正在尝试遍历文件树来计算文件数。但是即使我在我的 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)

最佳答案

您的类扩展了 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;
}

你调用父类(super class)的 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;
}

重新抛出之前处理过的相同异常。这就是为什么你得到异常(exception)。之后,您只会捕获到 AccessDeniedException,但正如我所见,您得到了一个 NoSuchFileException。

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

关于java - 如何防止 java FileTreeWalker 过早失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46203630/

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