确定两个文件名路径是否引用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();
}