Java Files isSameFile() Method

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

Definition

In Java, Files.isSameFile(a, b) returns true when both paths lead to the same file on disk, even if they are spelled differently: one with .. in it, one through a symbolic link, or two hard links to the same data.

Path.equals only compares the text of the paths. isSameFile asks the file system, which is why it can throw IOException.

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

Syntax

Pass two paths:

Java
Path a = dir.resolve("a.txt");
Path b = dir.resolve("sub").resolve("..").resolve("a.txt");
a.equals(b);               // false: different text
Files.isSameFile(a, b);    // true: same file
CallWhat it does
Files.isSameFile(a, b)Same file on disk?
a.equals(b)Same path text? No disk access.
a.normalize().equals(b.normalize())Text after removing . and ..; still no disk access.

Since Java 7. If both paths are equals, it returns true at once without checking that the file exists.

Library declarations
Java
public static boolean isSameFile(Path path, Path path2) throws IOException

Parameters

ParameterDescription
pathOne path.
path2The other path.

Return Value

Return ValueDescription
trueBoth locate the same file (or the paths are equal).
falseDifferent files.

Compare with Similar Methods

Comparing paths and files:

MethodHow it differs
createLink()Creates two names that are the same file.
readSymbolicLink()Shows where a link points.
readAllBytes()Compare content instead of identity.

Java Examples

Example 1 — Path.equals Says No, isSameFile Says Yes

A detour through sub/.. reaches the same file by a different path.

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

public class IsSameFileDetour {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path sub = Files.createDirectory(dir.resolve("sub"));
        Path a = Files.createFile(dir.resolve("a.txt"));
        Path detour = sub.resolve("..").resolve("a.txt");
        System.out.println("equals: " + a.equals(detour));
        System.out.println("equals after normalize: " + a.equals(detour.normalize()));
        System.out.println("isSameFile: " + Files.isSameFile(a, detour));
        Files.delete(a);
        Files.delete(sub);
        Files.delete(dir);
    }
}

How It Works

  1. Different text. The detour ends in sub/../a.txt, so the strings differ and equals is false.
  2. normalize cleans the text. Removing sub/.. makes the paths equal, but only as text; normalize never looks at the disk.
  3. Asking the disk. isSameFile resolves both paths on the file system and finds one and the same file.

Example 2 — Same Content Is Not the Same File

A copy holds equal bytes but is a different file; a hard link is 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 IsSameFileIdentity {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path photo = dir.resolve("photo.jpg");
        Files.write(photo, "pixels".getBytes(StandardCharsets.UTF_8));
        Path copy = Files.copy(photo, dir.resolve("copy.jpg"));
        Path link = Files.createLink(dir.resolve("link.jpg"), photo);
        System.out.println("photo vs copy: " + Files.isSameFile(photo, copy));
        System.out.println("photo vs link: " + Files.isSameFile(photo, link));
        Files.delete(link);
        Files.delete(copy);
        Files.delete(photo);
        Files.delete(dir);
    }
}

How It Works

  1. A real copy. Files.copy writes the same six bytes into a new, separate file.
  2. A second name. createLink adds link.jpg as another name for the original file's data.
  3. Identity, not content. The copy is a different file, so false; the hard link is the same file, so true.

Example 3 — Equal Missing Paths Are Fine, Unequal Ones Throw

Equal paths short-circuit; otherwise both files must exist.

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

public class IsSameFileMissing {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path real = Files.createFile(dir.resolve("real.txt"));
        Path ghost = dir.resolve("ghost.txt");
        System.out.println("ghost vs ghost: " + Files.isSameFile(ghost, ghost));
        try {
            Files.isSameFile(real, ghost);
        } catch (NoSuchFileException e) {
            System.out.println("real vs ghost: NoSuchFileException");
        }
        Files.delete(real);
        Files.delete(dir);
    }
}

How It Works

  1. A shortcut for equal paths. The two arguments are the same path, so the method returns true without looking for ghost.txt at all.
  2. Unequal paths need the disk. Comparing real.txt with ghost.txt requires reading both files' identities.
  3. Missing file, exception. ghost.txt cannot be found, so NoSuchFileException is thrown rather than a misleading false.

Frequently Asked Questions

equals compares path text without touching the disk. isSameFile checks whether both paths lead to the same file on disk, following links and resolving .. segments.
No. Two files with identical bytes are still different files. Compare the bytes with readAllBytes and Arrays.equals if content is what you need.
When the paths are not equal it has to look at both files, so a missing file causes an exception.
Yes. A symbolic link and its target are reported as the same file.
Yes. Hard links are different names for the same file, so isSameFile returns true.

Next: Files.isSymbolicLink()

Check whether a path itself is a symbolic link, whether or not its target exists.

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