Java File equals() Method
equals() The method is in Java's File an instance method provided by the class, used to compare whether two File objects point to the same file or directory. This method is inherited from the Object class, but is overridden in the File class to implement specific comparison logic.
Method Syntax
public
boolean
equals
(
Object
obj
)
Parameters
obj - the object to compare with the current File object
Return Value
If the two File objects represent the same file or directory, it returns true; otherwise it returns false
Method Characteristics
Path comparison
File The class's equals() The method compares the abstract paths of the two file objects, not the actual file content. It takes into account the case sensitivity of the path, which depends on the underlying operating system:
- In Unix/Linux systems, paths are case-sensitive
- In Windows systems, paths are case-insensitive
Normalization
Before comparison, the method normalizes the path, including:
- Converting separators in the path to the system default separator
- Resolving in relative paths
.and.. - Removing redundant separators
Difference from ==
equals() The method and the == operator are fundamentally different:
==compares whether two object references are the same (memory address)equals()compares whether the contents of two objects are the same
Usage examples
Example 1: Basic comparison
Example
import
java.io.File
;
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
)
)
;
// Outputs true
System
.
out
.
println
(
file1.
equals
(
file3
)
)
;
// Outputs false
}
}
Example 2: Relative vs. absolute path comparison
Example
import
java.io.File
;
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
)
)
;
// Outputs true
}
}
Example 3: Comparison of paths in different formats
Example
import
java.io.File
;
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
)
)
;
// Outputs true on Windows
}
}
Notes
equals()The method does not check whether the file actually exists in the file system; it only compares paths- If the argument is not a
Fileobject, the method returnsfalse - For symbolic links, the method compares the path of the symbolic link itself, not the target path it points to
- Pay attention to path case-sensitivity differences during cross-platform development
Relationship with hashCode()
File The class overrides the equals() method and also overrides the hashCode() method, ensuring that equal File objects have the same hash code. This is part of the Java object contract and is important for using File objects in hash collections.
Example
File
file1
=
new
File
(
"test.txt"
)
;
File
file2
=
new
File
(
"test.txt"
)
;
System
.
out
.
println
(
file1.
hashCode
(
)
==
file2.
hashCode
(
)
)
;
// Outputs true
Summary
File.equals() The method is an effective tool for comparing file paths; it provides more practical functionality than a 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 rather than actual file content — this is the key point to note when using this method.
Java File