Close

Java IO & NIO - Files.isSameFile() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

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

Tests whether two paths point to the same file.

Parameters:
path - one path to the file
path2 - the other path
Returns:
true if, and only if, the two paths locate the same file


Examples


package com.logicbig.example.files;

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

public class IsSameFileExample {

public static void main(String... args) throws IOException {
Path path = Files.createTempFile("test-file", ".txt");
Path otherPath = Paths.get(path.toString());
boolean sameFile = Files.isSameFile(path, otherPath);
System.out.println(sameFile);

Path anotherPath = Files.createTempFile("test-file", ".txt");
sameFile = Files.isSameFile(path, anotherPath);
System.out.println(sameFile);
}
}

Output

true
false




See Also