简体   繁体   English

Java 7 NIO2 Files.walk NotDirectoryException

[英]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. 我有一个SymbolicLink指向他的父目录,是怪异的,但是我试图理解FileSystemLoopException我已经做到了。 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. 我正在使用FollowLink选项,但是即使目标跟随在目录下,我也不知道,如果我未通过FOLLOW_LINK选项,则Files.walk说不是目录,为什么文件本身会打印出来,这是可以的,因为没有遵循。

In resume why Files.walk cant transverse my target which is a directory and is follow?? 在简历中,为什么Files.walk无法横穿我的目标,该目标是目录并在后面?

The API says API说

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? 但这是一个目录,并且仅跟随它是因为将其视为常规文件,它将打印文件名,而不是链接,并设置了该目录,但是Java无法识别为REGULAR目录?

My FileSystem is something like. 我的文件系统就像。

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

Most probably SymbolicApuntandoADirectorioLink.txt is a file symbolic link to the parent directory, instead of a directory symbolic link . 最有可能的是SymbolicApuntandoADirectorioLink.txtfile symbolic link父目录的file symbolic link ,而不是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 运行file.lnk的代码失败

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 运行dir.link的代码失败

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM