确定两个文件名路径是否引用Java中的同一文件
方法java.io.File.equals()用于查找两个文件名是否引用Java中的同一File。此方法需要一个参数,即要与另一个文件对象进行比较的文件对象。如果文件对象相同,则返回,否则返回false。
演示此的程序如下所示-
示例
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中的同一File。方法返回的布尔值将被打印。证明这一点的代码片段如下-
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();
}热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短