Java Files isSymbolicLink() Method

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

Definition

In Java, Files.isSymbolicLink(path) returns true when the entry at path is itself a symbolic link. It never follows the link, so it works even when the target is missing (a dangling link).

Regular files, folders, hard links, and missing paths all return false. Use it before deleting or walking, to treat links differently from real files, and pair it with readSymbolicLink to see the target.

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.isSymbolicLink(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the path; there are no options:

Java
if (Files.isSymbolicLink(p)) {
    Path target = Files.readSymbolicLink(p);
}
CallWhat it does
Files.isSymbolicLink(p)Is p a link?
Files.readSymbolicLink(p)Where does it point?
Files.exists(p)Does the target exist (follows the link)?

Since Java 7. Creating links in the examples may need Developer Mode or admin rights on Windows, so each example falls back to a message when creation fails.

Library declarations
Java
public static boolean isSymbolicLink(Path path)

Parameters

ParameterDescription
pathThe path to test.

Return Value

Return ValueDescription
trueThe entry is a symbolic link.
falseNot a link, missing, or unknown.

Compare with Similar Methods

Link helpers:

MethodHow it differs
createSymbolicLink()Creates a link.
readSymbolicLink()Reads its target.
isRegularFile()Follows links unless told not to.

Java Examples

Example 1 — Test a File, a Folder, a Missing Path, and a Link

Only the actual link answers true.

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

public class IsSymbolicLinkKinds {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.createFile(dir.resolve("real.txt"));
        Path link = dir.resolve("alias.txt");
        System.out.println("real.txt: " + Files.isSymbolicLink(file));
        System.out.println("temp folder: " + Files.isSymbolicLink(dir));
        System.out.println("missing: " + Files.isSymbolicLink(dir.resolve("nope")));
        try {
            Files.createSymbolicLink(link, file.getFileName());
            System.out.println("alias.txt: " + Files.isSymbolicLink(link));
        } catch (FileSystemException e) {
            System.out.println("alias.txt: could not be created here");
        } finally {
            Files.deleteIfExists(link);
            Files.delete(file);
            Files.delete(dir);
        }
    }
}

How It Works

  1. Ordinary entries. A file and a folder are not links, so both print false.
  2. No error for missing paths. nope does not exist, and the method just returns false.
  3. The link itself. alias.txt points to real.txt and is reported as a link; if links cannot be created, the fallback line appears instead.

Example 2 — Spot a Dangling Link

A link is broken when isSymbolicLink is true but exists, which follows the link, is false.

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

public class IsSymbolicLinkBroken {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path link = dir.resolve("old-logo.png");
        try {
            Files.createSymbolicLink(link, Paths.get("deleted-logo.png"));
            boolean broken = Files.isSymbolicLink(link) && !Files.exists(link);
            System.out.println("Is a link: " + Files.isSymbolicLink(link));
            System.out.println("Target exists: " + Files.exists(link));
            System.out.println("Broken: " + broken);
        } catch (FileSystemException e) {
            System.out.println("Could not create a link here");
        } finally {
            Files.deleteIfExists(link);
            Files.delete(dir);
        }
    }
}

How It Works

  1. A link to nothing. deleted-logo.png does not exist, but Java creates the link anyway.
  2. Two views. isSymbolicLink looks at the link entry (true), while exists follows it to the missing target (false).
  3. The broken-link rule. Combining the two answers is how tools find dangling links to report or remove.

Example 3 — Show "name -> target" for Links

List a folder, sorted, and print where each link points.

Java
import java.io.IOException;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class IsSymbolicLinkListing {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path v1 = Files.createFile(dir.resolve("app-1.0.jar"));
        Path current = dir.resolve("app.jar");
        try {
            Files.createSymbolicLink(current, v1.getFileName());
            List<Path> entries;
            try (Stream<Path> s = Files.list(dir)) {
                entries = s.sorted().collect(Collectors.toList());
            }
            for (Path p : entries) {
                String line = p.getFileName().toString();
                if (Files.isSymbolicLink(p)) {
                    line += " -> " + Files.readSymbolicLink(p);
                }
                System.out.println(line);
            }
        } catch (FileSystemException e) {
            System.out.println("Could not create a link here");
        } finally {
            Files.deleteIfExists(current);
            Files.delete(v1);
            Files.delete(dir);
        }
    }
}

How It Works

  1. Sorted entries. The listing is sorted so the output is stable; app-1.0.jar comes first because - sorts before the dot.
  2. Only links get an arrow. isSymbolicLink picks out app.jar, and readSymbolicLink supplies the stored target.
  3. Like ls -l. The result mirrors how Linux shows links, a common way to publish "the current version" of a file.

Frequently Asked Questions

Call Files.isSymbolicLink(path). It returns true only for the link entry itself and never follows the link.
Yes. It returns true for a link whose target is missing, which makes it the right tool for finding dangling links.
No. A hard link is just another name for a file, and isSymbolicLink returns false for it.
No. They are ordinary files that Windows Explorer interprets, so isSymbolicLink returns false.
Use Files.readSymbolicLink(link), which returns the stored target path.

Next: Files.isWritable()

Check whether a file exists and this program may change it.

isWritable() 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.

12 people found this page helpful