I am trying to use the FileWalkTree() method in JDK 7 (java.nio.file) .
To implement my own FileVisitor I have created a CustomFileVIsitor class by extending SimpleFileVisitor.
In this CustomFileVIsitor class …. I am overriding the function visitFile
public FileVisitResult visitFile(Path file, BasicFileAttributes fileStats)
throws IOException {
System.out.println("\t\tChecking Symbolic Link " + Files.isSymbolicLink(file));
System.out.println("\t\tChecking Symbolic Link " + fileStats.isSymbolicLink());
System.out.println("\t\tFile Size " + Files.size(file));
System.out.println("\t\tFile Size " + fileStats.size());
//Rest of the code
}
Now,to test this code, I have created a folder and then I added a file and a symbolic link (to a file) to it.
When I execute the class with FileWalkTree the output generated by Files.isSymbolicLink(file) and fileStats.isSymbolicLink() for the symbolic link is different
The output looks like this:
Checking Symbolic Link true
Checking Symbolic Link false
File Size 38
File Size 38
Why is it so ? Is it a bug or is there something I am missing in interpreting the information ?
I’m guessing that you are obtaining the file attributes via a call similar to
if that is so, you probably need to prevent the resolution of the symlink by passing in the appropriate
LinkOptionenumeration, like soOdds are you have resolved the link obtaining your
FileAttributes, which would explain why theFilereports it is a symlink while theFileAttributesreports it is not a symlink.