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
FileAttributes attrs = Files.readAttributes(path, FileAttributes.class);
if that is so, you probably need to prevent the resolution of the symlink by passing in the appropriate LinkOption enumeration, like so
FileAttributes attrs = Files.readAttributes(path, FileAttributes.class, NOFOLLOW_LINKS);
Odds are you have resolved the link obtaining your FileAttributes, which would explain why the Filereports it is a symlink while the FileAttributes reports it is not a symlink.
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