Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8287693

java.io.File.equals does not behave as expected

XMLWordPrintable

    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      Mac OSX

      A DESCRIPTION OF THE PROBLEM :
      MacOS has a case-insensitive file system. This causes unexpected results when using java.io.File.equals, which assumes case-sensitivity on all Unix systems.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      say there is a "file.txt" that exists on the MacOS filesystem.

      In bash, test -f file.txt and test -f FILE.txt both return true.

      Also in java, File("file.txt").exists() and File("FILE.txt") but return true.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Therefore, I expect File("file.txt") and File("FILE.txt") to pass an equality check. They both represent the same underlying content. Any filesystem operations (most of the methods in java.io.File) on these two objects will always have identical effects and results.
      ACTUAL -
      File("file.txt").eqauls(File("FILE.txt")) returns false

      ---------- BEGIN SOURCE ----------
      public class HelloWorld {

          public static void main(String[] args) {
              File file1 = new File("file.txt");
              File file2 = new File("file.txt");
              File file3 = new File("FILE.txt");
              System.out.println(file1.equals(file2));
              System.out.println(file1.equals(file3));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Creating a subclass of java.io.file and overriding the equals method

      FREQUENCY : always


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: