Java Files createLink() Method

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

Definition

In Java, Files.createLink(link, existing) creates a hard link: a new name, link, for the data of an existing file. Both names are equal; neither is "the original". Changes through one name are visible through the other.

The data stays on disk until the last name is deleted. Hard links work for regular files on the same drive or volume, on NTFS (Windows) and on typical macOS and Linux file systems. Note the argument order: the new name comes first.

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

Syntax

New link name first, existing file second:

Java
Path original = dir.resolve("original.txt");
Path alias = Files.createLink(dir.resolve("alias.txt"), original);
// alias.txt and original.txt now share the same data
CallWhat it does
Files.createLink(link, existing)Adds the name link for the file existing.
Files.isSameFile(link, existing)true: both names lead to one file.
Files.getAttribute(p, "unix:nlink")Number of names a file has (Linux/macOS only).

Since Java 7. A hard link is not a shortcut: there is no "target" to read back, and isSymbolicLink returns false. For a pointer to another path, use createSymbolicLink.

Library declarations
Java
public static Path createLink(Path link, Path existing) throws IOException

Parameters

ParameterDescription
linkThe new name to create; must not exist yet.
existingAn existing file, usually on the same file store.

Return Value

Return ValueDescription
PathThe link path, now another name for the file.

Compare with Similar Methods

Links and copies compared:

MethodHow it differs
createSymbolicLink()A pointer to a path; may dangle, may cross drives, needs privileges on Windows.
copy()Independent second copy of the data; changes are not shared.
isSameFile()Tells you whether two names lead to the same file.

Java Examples

Example 1 — Give a File a Second Name

Create a hard link, read the content through it, and confirm both names are the same file.

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

public class CreateLinkBasic {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path original = dir.resolve("original.txt");
        Files.write(original, "hello".getBytes(StandardCharsets.UTF_8));
        Path alias = Files.createLink(dir.resolve("alias.txt"), original);
        System.out.println("Link name: " + alias.getFileName());
        System.out.println("Alias content: " + new String(Files.readAllBytes(alias), StandardCharsets.UTF_8));
        System.out.println("Same file: " + Files.isSameFile(alias, original));
        System.out.println("Symbolic link: " + Files.isSymbolicLink(alias));
        Files.delete(alias);
        Files.delete(original);
        Files.delete(dir);
    }
}

How It Works

  1. Order of arguments. createLink(newName, existing): alias.txt is created and attached to the data of original.txt.
  2. One file, two names. Reading alias.txt returns hello, and isSameFile reports true because both entries refer to the same file on disk.
  3. Not a shortcut. isSymbolicLink is false: a hard link looks exactly like an ordinary file.

Example 2 — Edit Through One Name, Delete the Other

Append through the link, read through the first name, then delete the first name and see that the data survives.

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

public class CreateLinkShared {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path first = dir.resolve("first.txt");
        Files.write(first, "v1".getBytes(StandardCharsets.UTF_8));
        Path second = Files.createLink(dir.resolve("second.txt"), first);
        Files.write(second, " + more".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        System.out.println("Via first.txt: " + new String(Files.readAllBytes(first), StandardCharsets.UTF_8));
        Files.delete(first);
        System.out.println("first.txt exists: " + Files.exists(first));
        System.out.println("Via second.txt: " + new String(Files.readAllBytes(second), StandardCharsets.UTF_8));
        Files.delete(second);
        Files.delete(dir);
    }
}

How It Works

  1. Write through the link. Appending " + more" via second.txt changes the shared data, so first.txt now reads v1 + more as well.
  2. Delete removes a name, not the data. After Files.delete(first) that name is gone, but the data still has one name left.
  3. Last name standing. second.txt still returns the full text; only deleting it too frees the data.

Example 3 — Count the Names and Reuse a Taken Name

Read the link count through the Unix attribute view and try to create a link whose name already exists.

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

public class CreateLinkCount {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path data = Files.createFile(dir.resolve("data.bin"));
        Path link = Files.createLink(dir.resolve("data-link.bin"), data);
        try {
            Files.createLink(link, data);
        } catch (FileAlreadyExistsException e) {
            System.out.println("Name taken: FileAlreadyExistsException");
        }
        try {
            System.out.println("Link count: " + Files.getAttribute(data, "unix:nlink"));
        } catch (UnsupportedOperationException e) {
            System.out.println("Link count: no unix attribute view here");
        }
        Files.delete(link);
        Files.delete(data);
        Files.delete(dir);
    }
}

How It Works

  1. Existing names are protected. data-link.bin already exists, so a second createLink with that name throws FileAlreadyExistsException on every system.
  2. The link count. On Linux and macOS the attribute unix:nlink is an Integer with the number of names, 2 here.
  3. No unix view on Windows. Asking for an attribute view the platform does not have throws UnsupportedOperationException, so the program prints a fallback line.

Frequently Asked Questions

A second directory entry for the same file data, created with Files.createLink(newName, existing). Both names are equal, and the data is removed only when the last name is deleted.
A hard link is another name for the same data on the same drive and cannot dangle. A symbolic link stores a path to the target, can point to folders or other drives, can dangle, and needs extra privileges on Windows.
Yes, on NTFS drives, without administrator rights. FAT and exFAT drives do not support hard links.
Generally no. Windows, Linux, and macOS normally refuse hard links to folders, and the call throws an IOException.
On Linux and macOS read Files.getAttribute(path, "unix:nlink"), which returns an Integer. Windows has no unix view, so that call throws UnsupportedOperationException.

Next: Files.createSymbolicLink()

Create a symbolic link that points to another path (needs extra rights on Windows).

createSymbolicLink() 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