Java Files readSymbolicLink() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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
CallWhat 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

ParameterDescription
linkThe symbolic link.

Return Value

Return ValueDescription
PathThe target stored in the link, relative or absolute.

Compare with Similar Methods

Link methods:

MethodHow it differs
createSymbolicLink()Create the link.
isSymbolicLink()Is it a link at all?
isSameFile()Does the link lead to a given file?

Java Examples

Example 1 — Read a Relative Link and Resolve It

The link stores just a name; resolve it against the link's folder to reach the real file.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReadSymbolicLinkRelative {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path target = Files.write(dir.resolve("v2.txt"), "version 2".getBytes(StandardCharsets.UTF_8));
        Path link = dir.resolve("latest");
        try {
            Files.createSymbolicLink(link, Paths.get("v2.txt"));
            Path stored = Files.readSymbolicLink(link);
            System.out.println("Stored target: " + stored);
            System.out.println("Absolute: " + stored.isAbsolute());
            System.out.println("Target exists: " + Files.exists(link.resolveSibling(stored)));
            Files.delete(link);
        } catch (FileSystemException e) {
            System.out.println("Cannot create links here: " + e.getClass().getSimpleName());
        }
        Files.delete(target);
        Files.delete(dir);
    }
}

How It Works

  1. As written. The link was created with the bare name v2.txt, and readSymbolicLink returns exactly that.
  2. Relative to the link. resolveSibling joins the stored name with the link's folder, giving the real file.
  3. Privilege fallback. On Windows without Developer Mode, creating the link throws FileSystemException, so the program reports it and still cleans up.

Example 3 — Point a "current" Link at a New Version

Replace the link to switch from v1 to v2, then read through it.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReadSymbolicLinkRetarget {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path v1 = Files.write(dir.resolve("v1.txt"), "one".getBytes(StandardCharsets.UTF_8));
        Path v2 = Files.write(dir.resolve("v2.txt"), "two".getBytes(StandardCharsets.UTF_8));
        Path current = dir.resolve("current");
        try {
            Files.createSymbolicLink(current, Paths.get("v1.txt"));
            System.out.println("Before: " + Files.readSymbolicLink(current));
            Files.delete(current);
            Files.createSymbolicLink(current, Paths.get("v2.txt"));
            System.out.println("After: " + Files.readSymbolicLink(current));
            System.out.println("Content: " + new String(Files.readAllBytes(current), StandardCharsets.UTF_8));
            Files.delete(current);
        } catch (FileSystemException e) {
            System.out.println("Cannot create links here: " + e.getClass().getSimpleName());
        }
        Files.delete(v1);
        Files.delete(v2);
        Files.delete(dir);
    }
}

How It Works

  1. Links cannot be edited. To change a target, delete the link and create it again; delete removes only the link, never v1.txt.
  2. Read each version. readSymbolicLink shows v1.txt, then v2.txt.
  3. 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.

Next: Files.setAttribute()

Change one file attribute by name, such as a time or "dos:readonly".

setAttribute() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful