chiperortiz
chiperortiz

Reputation: 4991

Java 7 NIO2 Files.walk NotDirectoryException

I have a SymbolicLink pointing to his parent Directory yep is weird but i was trying to understand FileSystemLoopException i already did it. But another problem come out.

I have the following code.

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public final class FileLoopSystemException{
    private final Path target = Paths.get("C:\\Users\\Documents\\SymbolicLinks\\Folder\\SubDirectorio\\SymbolicApuntandoADirectorioLink.txt");
    public static void main(String[] args)throws Exception{
         final FileLoopSystemException clazz = new FileLoopSystemException();
         clazz.walk();
    }
    private void walk()throws Exception{
         System.out.println("Exist: "+Files.exists(target));//false
         System.out.println("Directory: "+Files.isDirectory(target));//true
         System.out.println("Symbolic: "+Files.isSymbolicLink(target));//true
         System.out.println("RegularFile: "+Files.isRegularFile(target));//false
         final long count = Files.walk(target,java.nio.file.FileVisitOption.FOLLOW_LINKS)
            .peek(a->System.out.println(Files.isSymbolicLink(a)+" "+a))
            .count();
         System.out.println("count = " + count);
    }        
}    

But throws

Exception in thread "main" java.nio.file.NotDirectoryException: C:\Users\Documents\SymbolicLinks\Folder\SubDirectorio\SymbolicApuntandoADirectorioLink.txt

I am using the FollowLink Option but i dont know even when the target is follow which is a directory why Files.walk says is not a directory if i not pass the FOLLOW_LINK option is printed the file itself which is OK because is not follow.

In resume why Files.walk cant transverse my target which is a directory and is follow??

The API says

Checked exception thrown when a file system operation, intended for a directory, fails because the file is not a directory.

But it is a directory and is follow just because is was treat it as a regular file it would print the file name instead the link is follow and the directory is set but Java doesn't recognize as a REGULAR directory?

My FileSystem is something like.

C:\Users\Documents\SymbolicLinks\Folder\SubDirectorio
   ....SymbolicApuntandoADirectorioLink.txt -->SymbolicLink pointing to parent directory SubDirectorio

Upvotes: 1

Views: 789

Answers (1)

SubOptimal
SubOptimal

Reputation: 22973

Most probably SymbolicApuntandoADirectorioLink.txt is a file symbolic link to the parent directory, instead of a directory symbolic link.

cd c:\temp
mkdir dir1
cd dir1
mklink file.link ..
mklink /d dir.link ..
dir
...
11/06/2018  15:03    <SYMLINKD>     dir.link [..]
11/06/2018  15:02    <SYMLINK>      file.link [..]

Running your code for file.lnk fails with

Exist: false
Directory: true
Symbolic: true
RegularFile: false
Exception in thread "main" java.nio.file.NotDirectoryException: C:\temp\dir1\file.link    

Running your code for dir.link fails with

Exist: true
Directory: true
Symbolic: true
RegularFile: false
true C:\temp\dir1\dir.link
Exception in thread "main" java.io.UncheckedIOException: java.nio.file.FileSystemLoopException: C:\temp\dir1\dir.link\dir1\dir.link

Upvotes: 2

Related Questions