Java Example - Comparing File Paths
The following example demonstrates how to use the File class's filename.compareTo(another filename) method to compare whether two file paths are in the same directory:
Main.java File
import
java
.
io
.
File
;
public
class
Main
{
public
static
void
main
(
String
[
]
args
)
{
File
file1
=
new
File
(
"
C:/File/demo1.txt
"
)
;
File
file2
=
new
File
(
"
C:/java/demo1.txt
"
)
;
if
(
file1
.
compareTo
(
file2
)
==
0
)
{
System
.
out
.
println
(
"
The file paths are the same!
"
)
;
}
else
{
System
.
out
.
println
(
"
The file paths are different!
"
)
;
}
}
}
The output of the above code is:
文件路径不一致!
Java Examples