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

new File("").exists() returns false whereas it is the current working directory

XMLWordPrintable

    • b12
    • generic

      A DESCRIPTION OF THE REQUEST :
      If you do a:
      new File("").exists();

      it returns false, can cause irritations as it tells you the abstract path name if you print out:
      new File("").getAbsolutePath();

      So it should rather return true - which it does if you type:
      File f = new File("");
      new File(f.getAbsolutePath()).exists();

      JUSTIFICATION :
      It causes irrtitations about the function of exists, as this is not mentioned in the method comment of exists(), but it is stated, that an empty path returns the working directory.

      If you are not aware of this behaviour, it can lead to severe control flow errors in your code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      return true on empty path
      ACTUAL -
      returns false on obviously existing path

      ---------- BEGIN SOURCE ----------
      File f = new File("");
      if(!f.exists())
      System.out.println(f.getAbsolutePath());
      System.out.println("But it does exist: " + new File(f.getAbsolutePath()).exists());

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      File f = new File("");
      new File(f.getAbsolutePath()).exists();

            bpb Brian Burkhalter
            coffeys Sean Coffey
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: