简体   繁体   中英

Checking if a file or directory exists in Java

From this java tutorial by oracle:

Note that !Files.exists(path) is not equivalent to Files.notExists(path).

Why would they not be equivalent? it does not go any further in terms of explanation. Does anybody know more information about that? Thanks in advance!

!Files.exists() returns:

  • true if the file does not exist or its existence cannot be determined
  • false if the file exists

Files.notExists() returns:

  • true if the file does not exist
  • false if the file exists or its existence cannot be determined

As we see from Files.exists the return result is:

TRUE if the file exists; 
FALSE if the file does not exist or its existence cannot be determined.

And from Files.notExists the return result is:

TRUE if the file does not exist; 
FALSE if the file exists or its existence cannot be determined

So if !Files.exists(path) return TRUE means it not exists or the existence cannot be determined (2 possibilities) and for Files.notExists(path) return TRUE means it not exists (just 1 possibility).

The conclusion !Files.exists(path) != Files.notExists(path) or 2 possibilities not equals to 1 possibility (see the explanation above about the possibilities).

Looking in the source code for the differences they both do the exact same thing with 1 major difference. The notExist(...) method has an extra exception to be caught.

Exist:

public static boolean exists(Path path, LinkOption... options) {
    try {
        if (followLinks(options)) {
            provider(path).checkAccess(path);
        } else {
            // attempt to read attributes without following links
            readAttributes(path, BasicFileAttributes.class,
                           LinkOption.NOFOLLOW_LINKS);
        }
        // file exists
        return true;
    } catch (IOException x) {
        // does not exist or unable to determine if file exists
        return false;
    }

}

Not Exist:

public static boolean notExists(Path path, LinkOption... options) {
    try {
        if (followLinks(options)) {
            provider(path).checkAccess(path);
        } else {
            // attempt to read attributes without following links
            readAttributes(path, BasicFileAttributes.class,
                           LinkOption.NOFOLLOW_LINKS);
        }
        // file exists
        return false;
    } catch (NoSuchFileException x) {
        // file confirmed not to exist
        return true;
    } catch (IOException x) {
        return false;
    }
}

As a result the differences are as follow:

  • !exists(...) returns the file as non existent if an IOException is thrown when attempting to retrieve the file.

  • notExists(...) returns the file as non existent by making sure the specific IOException subexception NoSuchFileFound is thrown and that it isn't any other subexception on IOException causing the not found result

From the Oracle docs of notExists .

Note that this method is not the complement of the exists method. Where it is not possible to determine if a file exists or not then both methods return false . ...

My highlighting.

You can just specify the absolute path, if the directory/directories doesn't exits, it will create the drectory/directories.

private void createDirectoryIfNeeded(String directoryName)
{
File theDir = new File(directoryName); 
if (!theDir.exists())
    theDir.mkdirs();
}
 import java.io.File;

 // Create folder   
 boolean isCreate = new File("/path/to/folderName/").mkdirs();

 // check if exist 
 File dir = new File("/path/to/newFolder/");
 if (dir.exists() && dir.isDirectory()) {

 //the folder exist..
 }

Can also check if Boolean variable == True , But this check is better and more effective.

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