I'm using Files.walkFileTree to delete a directory. When running this code in CentOS, I got an error in my server log.
if (Files.exists(p)) {
if (Files.isDirectory(p)) {
Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
try {
Files.delete(file);
} catch (NoSuchFileException ignored) {
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e)
throws IOException {
if (e == null) {
try {
Files.delete(dir);
} catch (NoSuchFileException ignored) {
}
return FileVisitResult.CONTINUE;
} else {
// directory iteration failed
throw e;
}
}
});
}
Caused by: java.nio.file.NoSuchFileException: /home/cluster/apache-tomcat-9.0.21/webapps/webroot/spider/db/copyT_92A501/super/P-1/S-1/day_of_year-col-6-dic
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86) ~[?:1.8.0_202]
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102) ~[?:1.8.0_202]
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107) ~[?:1.8.0_202]
at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55) ~[?:1.8.0_202]
at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:144) ~[?:1.8.0_202]
at sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99) ~[?:1.8.0_202]
at java.nio.file.Files.readAttributes(Files.java:1737) ~[?:1.8.0_202]
at java.nio.file.FileTreeWalker.getAttributes(FileTreeWalker.java:219) ~[?:1.8.0_202]
at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:276) ~[?:1.8.0_202]
at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372) ~[?:1.8.0_202]
at java.nio.file.Files.walkFileTree(Files.java:2706) ~[?:1.8.0_202]
at java.nio.file.Files.walkFileTree(Files.java:2742) ~[?:1.8.0_202]
I searched some keywords on the internet and there is no answer yet, maybe something is wrong with my code. So I wonder is it because Files.walkFileTree is not thread safe or something? I think maybe there is another thread changing the file here (maybe). Or maybe because of some thread is reading the files inside at that moment?