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
Call
What 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
Parameter
Description
path
One path.
path2
The other path.
Return Value
Return Value
Description
true
Both locate the same file (or the paths are equal).
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);
}
}
Output
Run with java IsSameFileDetour. The output is the same on Windows, macOS, and Linux.
equals: false
equals after normalize: true
isSameFile: true
How It Works
Different text. The detour ends in sub/../a.txt, so the strings differ and equals is false.
normalize cleans the text. Removing sub/.. makes the paths equal, but only as text; normalize never looks at the disk.
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);
}
}
Output
Run with java IsSameFileIdentity. Same output on Windows (NTFS), macOS, and Linux.
photo vs copy: false
photo vs link: true
How It Works
A real copy.Files.copy writes the same six bytes into a new, separate file.
A second name.createLink adds link.jpg as another name for the original file's data.
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);
}
}
Output
Run with java IsSameFileMissing. The output is the same on Windows, macOS, and Linux.
ghost vs ghost: true
real vs ghost: NoSuchFileException
How It Works
A shortcut for equal paths. The two arguments are the same path, so the method returns true without looking for ghost.txt at all.
Unequal paths need the disk. Comparing real.txt with ghost.txt requires reading both files' identities.
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.