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 an actual folder I was expecting it to just skip over it.
I cannot see any options that let me control this behaviour
Code:
package com.jthink.songkong.fileloader;
import com.jthink.songkong.cmdline.SongKong;
import com.jthink.songkong.ui.MainWindow;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.Callable;
import java.util.logging.Level;
/**
* Count the number of files that can be loaded, for information purposes only
*/
public class CountFilesinFolder implements Callable<Boolean> {
public static class CountFiles
extends SimpleFileVisitor<Path> {
private int fileCount = 0;
private final PathMatcher matcher;
CountFiles(String pattern) {
matcher =
FileSystems.getDefault()
.getPathMatcher("regex:" + pattern);
}
/**
* Find Music file
*
* @param file
* @param attr
* @return
*/
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attr) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
fileCount++;
}
return FileVisitResult.CONTINUE;
}
public int getFileCount() {
return fileCount;
}
}
private Path scanDir;
public CountFilesinFolder(Path scanDir) {
this.scanDir = scanDir;
}
public Boolean call() {
CountFiles countFiles = null;
try {
countFiles = new CountFiles("^(?!._).*[.](?:mp3|mp4|m4p|m4b|m4a|ogg|flac|wma)$");
Files.walkFileTree(scanDir, countFiles);
}
catch (Exception e) {
MainWindow.logger.log(Level.SEVERE, "Unable to find file for deriving base folder", e);
}
MainWindow.logger.severe("Music File Count:"+countFiles.getFileCount());
SongKong.setMaxProgress(countFiles.getFileCount());
return true;
}
}
gives this stacktrace
java.nio.file.NoSuchFileException: Z:\Scratch\fred.tar
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:526)
at java.nio.file.Files.newDirectoryStream(Files.java:411)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:179)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:199)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:69)
at java.nio.file.Files.walkFileTree(Files.java:2591)
at java.nio.file.Files.walkFileTree(Files.java:2624)
at com.jthink.songkong.fileloader.CountFilesinFolder.call(CountFilesinFolder.java:68)
at com.jthink.songkong.fileloader.CountFilesinFolder.call(CountFilesinFolder.java:15)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
but that is a a remote drive (nas drive), I get no such error on local drive
EDIT Implemented the following based o the answer below I thought worked
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs)
throws IOException {
if(dir.endsWith(".tar"))
{
return FileVisitResult.SKIP_SUBTREE;
}
return super.preVisitDirectory(dir, attrs);
}
but my testing was amiss, it doesnt in fact work because the code in FileTreeWalker that fails is called before the previsit method
try {
DirectoryStream<Path> stream = null;
FileVisitResult result;
// open the directory
try {
stream = Files.newDirectoryStream(file);
} catch (IOException x) {
return visitor.visitFileFailed(file, x);
} catch (SecurityException x) {
// ignore, as per spec
return FileVisitResult.CONTINUE;
}
// the exception notified to the postVisitDirectory method
IOException ioe = null;
// invoke preVisitDirectory and then visit each entry
try {
result = visitor.preVisitDirectory(file, attrs);
if (result != FileVisitResult.CONTINUE) {
return result;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With