简体   繁体   中英

Can a java File that isFile() also be isDirectory()?

The following tests passes:

    File aDir = new File("aDir");
    assertTrue(aDir.exists());
    assertTrue(aDir.isDirectory());
    assertFalse(aDir.isFile());

    File aFile = new File("aFile");
    assertTrue(aFile.exists());
    assertFalse(aFile.isDirectory());
    assertTrue(aFile.isFile());

    File awol = new File("notInFileSystem");
    assertFalse(awol.exists());
    assertFalse(awol.isDirectory());
    assertFalse(awol.isFile());

On the surface of things, And it seems to imply that for all files where file.isFile() is true, file.isDirectory() is false. Is there any known type of file system/file type/java platform where this assumption does not hold?

(There are all sorts of wild in-betweeen categories of files (symlinks, junction points, symlinks/junction points with missing targets etc) that may behave slightly differently)

Looking at JavaDoc, this seems to be always the case:

http://docs.oracle.com/javase/7/docs/api/java/io/File.html#isFile()

isDirectory:

true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise

isFile:

true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

From the documentation :

isFile() :

Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.

This definitely implies that if isFile() is true then isDirectory() must be false . Based on the wording in the isDirectory() doc, the inverse is true as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM