Reputation: 811
I've been playing with java.nio.file.Files
and stumbled upon a strange issue. I have a symbolic link, but Files.isSymbolicLink()
and symbolic link attribute of Files.readAttributes()
show different results.
Here's how I create the link:
D:\DEV\test>mklink /D link1 components
symbolic link created for link1 <<===>> components
Relevant java code:
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
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?
Upvotes: 6
Views: 4688
Reputation: 14085
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.
DosFileAttributes dosFileAttributes = Files.readAttributes(symLinkDirectory,
DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
Upvotes: 15