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?
Developers use Java to construct applications in laptops, data centres, game consoles, scientific supercomputers, cell phones, and other devices. Java is the world's third most popular programming language, after Python and C – according to the TIOBE index, which evaluates programming language popularity.
Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today's digital world, by providing the reliable platform upon which many services and applications are built.
The Java™ Programming Language is a general-purpose, concurrent, strongly typed, class-based object-oriented language. It is normally compiled to the bytecode instruction set and binary format defined in the Java Virtual Machine Specification.
Java is easy to learn. Java was designed to be easy to use and is therefore easy to write, compile, debug, and learn than other programming languages. Java is object-oriented. This allows you to create modular programs and reusable code.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With