Files.walkFileTree does not enumerate files which are created while traversing (Win)
Rajendra Gutupalli
Rajendra.Gutupalli at Sun.COM
Tue Aug 5 06:46:05 PDT 2008
Hi Alan,
I have tried to see whether Files.walkFileTree() lists the files which
are created while traversing a directory. In preVisitDirectory() method
I have created a file to see whether visistFile() methods gets a hit for
the newly created file. In Solaris and Linux the visitFile() method is
called for the newly created file but in Windows it does not get called.
Please see the following result and code in Linux and Windows XP. In
Windows XP I got the DNEE since the created a file is not deleted in
visistFile() method and I am trying to delete the directory in
postVisitDirectory.
<WindowsXP>
C:\build\classes>java adhoc.TreeTest04
pre dir TestDir
post dir TestDir
java.nio.file.DirectoryNotEmptyException: TestDir
at sun.nio.fs.WindowsPath.delete(WindowsPath.java:767)
at
adhoc.TreeTest04$MyFileVisitor.postVisitDirectory(TreeTest04.java:63)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:196)
at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:58)
at java.nio.file.Files.walkFileTree(Files.java:392)
at adhoc.TreeTest04.test01(TreeTest04.java:31)
at adhoc.TreeTest04.main(TreeTest04.java:23)
</WindowsXP>
<Linux>
bash-3.00$ ./jdk1.7.0/bin/java TreeTest04
pre dir TestDir
visit file TestDir/testfile
post dir TestDir
</Linux>
<code>
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;
import java.util.Set;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitOption.*;
public class TreeTest04 {
public static void main(String... args) {
TreeTest04.test01();
}
private static void test01() {
Path path = Paths.get("TestDir");
try {
path.createDirectory();
Set<FileVisitOption> visitOpts = EnumSet.of(DETECT_LOOPS,
FOLLOW_LINKS);
Files.walkFileTree(path, visitOpts, -1, new MyFileVisitor());
} catch (IOException e) {
e.printStackTrace();
}
}
static class MyFileVisitor implements FileVisitor {
public FileVisitResult preVisitDirectory(FileRef dir, Path
relPath) {
System.out.println("pre dir " + dir);
Path file = ((Path) dir).resolve("testfile");
try {
file.createFile();
} catch (IOException e) {
e.printStackTrace();
}
return CONTINUE;
}
public FileVisitResult visitFile(FileRef file, Path relPath,
BasicFileAttributes attrs) {
System.out.println("visit file " + file);
try {
file.delete(true);
}catch(IOException e){
e.printStackTrace();
}
return CONTINUE;
}
public FileVisitResult postVisitDirectory(FileRef dir, Path
relPath) {
System.out.println("post dir " + dir);
try {
dir.delete(true);
} catch (IOException e) {
e.printStackTrace();
}
return CONTINUE;
}
public FileVisitResult visitFileFailed(FileRef file, Path
relPath, IOException exc) {
System.out.println("file failed " + file);
exc.printStackTrace();
return CONTINUE;
}
public FileVisitResult visitDirectoryFailed(FileRef dir, Path
relPath, IOException exc) {
System.out.println("pre dir " + dir);
return CONTINUE;
}
}
}
</code>
Thanks
Rajendra.
More information about the nio-dev
mailing list