JDK-6192331 : File.listFiles() returns files which "don't exist" (win)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-11-08
  • Updated: 2015-08-06
  • Resolved: 2006-02-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b73Fixed
Related Reports
Relates :  
Relates :  
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

Comments
EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=10441
06-12-2005

EVALUATION hiberfil.sys is the file that Windows creates when the computer goes into hibernation mode. I suspect that GetFileAttributes is returning an error (probably sharing violation) when we attempt to obtain its attributes. There is already a hack in getBooleanAttributes to deal with pagefile.sys.
04-10-2005