我有一个 SymbolicLink 指向他的父目录,是的,这很奇怪,但我试图理解 FileSystemLoopException 我已经做到了。但另一个问题又出现了。
我有以下代码。
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);
}
}
但是抛出
Exception in thread "main" java.nio.file.NotDirectoryException: C:\Users\Documents\SymbolicLinks\Folder\SubDirectorio\SymbolicApuntandoADirectorioLink.txt
我正在使用FollowLink选项,但即使目标是follow,我也不知道这是一个目录,为什么Files.walk说不是一个目录,如果我不通过FOLLOW_LINK选项,则会打印文件本身,这是可以的,因为不是关注。
在简历中,为什么 Files.walk 无法横贯我的目标(这是一个目录并且是跟随的)?
API 说
Checked exception thrown when a file system operation, intended for a directory, fails because the file is not a directory.
但是它是一个目录并且被跟踪只是因为将其视为常规文件它会打印文件名而不是链接被跟踪并且目录已设置但Java无法识别为常规目录?
我的文件系统类似于。
C:\Users\Documents\SymbolicLinks\Folder\SubDirectorio
....SymbolicApuntandoADirectorioLink.txt -->SymbolicLink pointing to parent directory SubDirectorio
最佳答案
很可能SymbolicApuntandoADirectorioLink.txt
是到父目录的文件符号链接(symbolic link)
,而不是目录符号链接(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 [..]
运行 file.lnk
代码失败,并显示
Exist: false
Directory: true
Symbolic: true
RegularFile: false
Exception in thread "main" java.nio.file.NotDirectoryException: C:\temp\dir1\file.link
运行 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
关于Java 7 NIO2 Files.walk NotDirectoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797796/