确定 Java 中的两个文件名路径是否指向同一文件
java.io.File.equals() 方法用于判断 Java 中两个文件名是否指向同一文件。此方法需要一个参数,即要与另一个文件对象进行比较的文件对象。如果文件对象相同,则返回真,否则返回假。
演示此方法的程序如下 −
示例
import java.io.File;
public class Demo {
public static void main(String[] args) {
try {
File file1 = new File("demo1.txt");
File file2 = new File("demo2.txt");
boolean flag = file1.equals(file2);
System.out.print("The two file names refer to the same file? " + flag);
} catch(Exception e) {
e.printStackTrace();
}
}
}以上程序的输出如下 −
输出
The two file names refer to the same file? false
现在让我们了解一下以上程序。
java.io.File.equals() 方法用于判断 Java 中两个文件名是否指向同一文件。打印由该方法返回的布尔值。演示此方法的代码片段如下 −
try {
File file1 = new File("demo1.txt");
File file2 = new File("demo2.txt");
boolean flag = file1.equals(file2);
System.out.print("The two file names refer to the same file? " + flag);
} catch(Exception e) {
e.printStackTrace();
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP