在我的 Mac OS X 上,我有以下文件层次结构:
(base) bohaoli@bunnyBoom-2:~/Desktop/tuto/tuto_link$ tree
.
├── a
│ └── b
│ ├── c
│ │ └── d
│ └── link -> /Users/bohaoli/Desktop/tuto/tuto_link/e/f/g/h/cousin.txt
└── e
└── f
└── g
└── h
└── cousin.txt
但是下面代码的最后两行
Path path = Paths.get("/Users/bohaoli/Desktop/tuto/tuto_link/a");
Files.walk(path, FileVisitOption.FOLLOW_LINKS).forEach(System.out::println);
Files.walk(path).forEach(System.out::println);
给出相同的结果:
/Users/bohaoli/Desktop/tuto/tuto_link/a
/Users/bohaoli/Desktop/tuto/tuto_link/a/b
/Users/bohaoli/Desktop/tuto/tuto_link/a/b/link
/Users/bohaoli/Desktop/tuto/tuto_link/a/b/c
/Users/bohaoli/Desktop/tuto/tuto_link/a/b/c/d
为什么即使我提供了 FileVisitOption.FOLLOW_LINKS 选项,链接也没有被关注?
最佳答案
FileVisitOption.FOLLOW_LINKS
仅适用于符号目录链接。否则您期望的结果是什么(您的结果中已经有 /Users/bohaoli/Desktop/tuto/tuto_link/a/b/link
)。访问节点时,您可以使用这些方法处理文件符号链接(symbolic link):
boolean Files.isSymbolicLink(file.toPath())
File getCanonicalFile()
String getCanonicalPath()
关于java - 为什么 Files.walk(path, FileVisitOption.FOLLOW_LINKS) 不关注链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59468074/