In Java, Files.readSymbolicLink(link) returns the target path stored in the link, without following it. If the link was created with a relative target such as v2.txt, you get exactly v2.txt back, not a full path.
A relative target is interpreted from the link's own folder, so resolve it with link.resolveSibling(target). The target does not have to exist: dangling links can still be read.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.readSymbolicLink(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Pass the link itself:
Java
Path stored = Files.readSymbolicLink(link);
Path actual = link.resolveSibling(stored); // works for relative and absolute targets
Call
What it does
Files.readSymbolicLink(link)
Stored target, as written.
link.resolveSibling(target)
Target as a usable path.
Files.isSymbolicLink(p)
Check before reading.
Since Java 7. On Windows, creating links needs Developer Mode or administrator rights, but reading an existing link does not.
Library declarations
Java
public static Path readSymbolicLink(Path link) throws IOException
Parameters
Parameter
Description
link
The symbolic link.
Return Value
Return Value
Description
Path
The target stored in the link, relative or absolute.
Run with java ReadSymbolicLinkRelative. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
Stored target: v2.txt
Absolute: false
Target exists: true
--- Windows without the symbolic-link privilege ---
Cannot create links here: FileSystemException
How It Works
As written. The link was created with the bare name v2.txt, and readSymbolicLink returns exactly that.
Relative to the link.resolveSibling joins the stored name with the link's folder, giving the real file.
Privilege fallback. On Windows without Developer Mode, creating the link throws FileSystemException, so the program reports it and still cleans up.
Example 2 — NotLinkException and NoSuchFileException
Reading a normal file or a missing path fails. This example needs no link privileges.
Run with java ReadSymbolicLinkRetarget. First part: Linux, macOS, or Windows with Developer Mode. Last part: Windows without the symbolic-link privilege.
Before: v1.txt
After: v2.txt
Content: two
--- Windows without the symbolic-link privilege ---
Cannot create links here: FileSystemException
How It Works
Links cannot be edited. To change a target, delete the link and create it again; delete removes only the link, never v1.txt.
Read each version.readSymbolicLink shows v1.txt, then v2.txt.
Reading through the link.readAllBytes(current) follows the link and returns the content of v2.txt.
Frequently Asked Questions
Files.readSymbolicLink(link). Resolve a relative result with link.resolveSibling(target).
It returns the target exactly as it was stored when the link was created.
The exception readSymbolicLink throws when the path is not a symbolic link.
No. Dangling links can be read; only the link itself must exist.
Call path.toRealPath(), which follows every link and returns the absolute final path.