[英]Java & symbolic links in windows
I've been playing with java.nio.file.Files
and stumbled upon a strange issue. 我一直在玩java.nio.file.Files
并偶然发现了一个奇怪的问题。 I have a symbolic link, but Files.isSymbolicLink()
and symbolic link attribute of Files.readAttributes()
show different results. 我有一个符号链接,但Files.isSymbolicLink()
和符号链接属性Files.readAttributes()
显示不同的结果。
Here's how I create the link: 这是我创建链接的方式:
D:\DEV\test>mklink /D link1 components
symbolic link created for link1 <<===>> components
Relevant java code: 相关的java代码:
Path symLinkDirectory = Paths.get("D:\\DEV\\test\\link1");
DosFileAttributes dosFileAttributes = Files.readAttributes(symLinkDirectory, DosFileAttributes.class);
System.out.println(String.format(
"Files.isSymbolicLink(): %b, dosFileAttributes.isSymbolicLink(): %b",
Files.isSymbolicLink(symLinkDirectory), dosFileAttributes.isSymbolicLink()));
Gives me this output: 给我这个输出:
Files.isSymbolicLink(): true, dosFileAttributes.isSymbolicLink(): false Files.isSymbolicLink():true,dosFileAttributes.isSymbolicLink():false
Could anyone tell me why attributes report that the file is not a symbolic link? 谁能告诉我为什么属性报告文件不是符号链接? Am I missing something? 我错过了什么吗? Is this happening on unix too? 这也发生在unix上吗?
You need to add LinkOption.NOFOLLOW_LINKS
to the invocation of readAttributes to get the attributes of the link itself instead of the link target. 您需要将LinkOption.NOFOLLOW_LINKS
添加到LinkOption.NOFOLLOW_LINKS
的调用中,以获取链接本身的属性而不是链接目标。
DosFileAttributes dosFileAttributes = Files.readAttributes(symLinkDirectory,
DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.