Java File equals() Method
equals()The method is in JavaFileAn instance method provided by the class, used to compare twoFileobjects to see if they point to the same file or directory. This method is inherited fromObjectthe class, butFileit is overridden in the class to implement specific comparison logic.
Method Syntax
Parameters
obj- To be compared with the currentFileobject to be compared
Return Value
If twoFileobjects represent the same file or directory, then returntrue; otherwise, returnfalse
Method Features
Path Comparison
FileThe class'sequals()method compares the abstract paths of two file objects, not the actual file contents. It considers the case sensitivity of the paths, which depends on the underlying operating system:
- On Unix/Linux systems, paths are case-sensitive
- On Windows systems, paths are case-insensitive
Normalization
Before comparison, the method normalizes the paths, including:
- Converting the path separators to the system default separator
- Resolving the relative path's
.and.. - Removing redundant separators
Difference from ==
equals()The method==has an essential difference from the == operator:
==Compares whether the references of two objects are the same (memory address)equals()Compares whether the contents of two objects are the same
Usage Examples
Example 1: Basic Comparison
Example
public class FileEqualsExample {
public static void main(String[] args) {
File file1 = new File("test.txt");
File file2 = new File("test.txt");
File file3 = new File("another.txt");
System.out.println(file1.equals(file2)); // Output true
System.out.println(file1.equals(file3)); // Output false
}
}
Example 2: Comparing Relative Path and Absolute Path
Example
public class RelativeAbsoluteComparison {
public static void main(String[] args) {
File relativeFile = new File("test.txt");
File absoluteFile = new File(relativeFile.getAbsolutePath());
System.out.println(relativeFile.equals(absoluteFile)); // Output true
}
}
Example 3: Comparing Different Path Formats
Example
public class DifferentPathFormats {
public static void main(String[] args) {
// On Windows systems
File file1 = new File("C:\\temp\\test.txt");
File file2 = new File("C:/temp/test.txt");
System.out.println(file1.equals(file2)); // Output true on Windows
}
}
Notes
equals()The method does not check whether the files actually exist in the file system; it only compares paths- If the argument is not
Filean object, the method returnsfalse - For symbolic links, the method compares the path of the symbolic link itself, not the path to which it points
- During cross-platform development, pay attention to differences in path case sensitivity
Relationship with hashCode()
FileThe class overridesequals()the method and also overrideshashCode()the method, ensuring that equalFileobjects have the same hash code. This is part of the Java object contract and is important for usingFileobjects in hash collections.
Example
File file2 = new File("test.txt");
System.out.println(file1.hashCode() == file2.hashCode()); // Output true
Summary
File.equals()The method is an effective tool for comparing file paths, providing more practical functionality than simple reference comparison. Understanding how it works helps correctly handle file path comparisons and avoid logical errors in file operations. Remember that it compares abstract paths, not actual file contents, which is the key point to note when using this method.
Java File