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

File.listFiles() returns files which "don't exist" (win)

    Details

    • Subcomponent:
    • Resolved In Build:
      b73
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      NTFS filesystem

      A DESCRIPTION OF THE PROBLEM :
      When obtaining a list of files on "C:/", the listFiles() method returns files which, when exists() is called, returns false.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      This is noticeable on the hiberfil.sys file on the root of C:. If you don't have this file, you probably won't be able to reproduce the problem unless you find a file which behaves similarly.

      Compile and run the provided sample program.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should show "exists" next to every file in the list.

      ACTUAL -
      The file "C:\hiberfil.sys" says "DOES NOT EXIST", as File.exists() returned false for that file, despite it having been returned in File.listFiles()

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {
          public static void main (String args[]) {
              File root = new File("C:\\");
              System.out.println("Root is " + root);

              File[] dir = root.listFiles();
              for (int i = 0; i < dir.length; i++) {
                  System.out.print(" dir[" + i + "] = " + dir[i] + " ");
                  if (dir[i].exists()) {
                      System.out.println("exists");
                  } else {
                      System.out.println("DOES NOT EXIST!");
                  }
              }
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      It's ugly, but there are two options.

      Option one, have a way to return a File array which is already sanitised:

      public static File[] listFilesFixed(File parent) {
          File[] list = parent.listFiles();
          List fixedList = new ArrayList();
          for (int i = 0; i < list.length; i++) {
              if (list[i].exists()) {
                  fixedList.add(list[i]);
              }
          }
          return (File[]) fixedList.toArray(new File[fixedList.size()]);
      }

      Option two is to take the result of listFiles() with a shaker of salt, and check exists() on every single file in the list.
      ###@###.### 2004-11-08 21:08:40 GMT

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sherman Xueming Shen
                Reporter:
                rmandalasunw Ranjith Mandala (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: