Checking for a file or directory in Java

From this java tutorial from oracle:

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

Why are they not equivalent? this does not go further in terms of explanation. Does anyone know more about this? Thanks in advance!

+6
source share
6 answers

!Files.exists() returns:

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

Files.notExists() returns:

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

As seen 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 that it does not exist or existence cannot be defined (2 possibilities), and for Files.notExists(path) return TRUE means that it does not exist (just one possibility) .

Conclusion !Files.exists(path) != Files.notExists(path) or 2 possibilities not equals to 1 possibility (see explanation above on possibilities).

+3
source

Looking in the source code for differences, they both do the same with 1 big difference. The notExist(...) method has an additional exception that can 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; } } 

Does 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 follows:

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

  • notExists(...) returns the file as non-existent, making sure that a specific IOException subexception NoSuchFileFound and that it is not another sub-element of the IOException , raising an NoSuchFileFound result

+3
source

From Oracle docs notExists .

Note that this method is not in addition to the exist method. If it is not possible to determine whether the file exists or not, both methods return false ....

My highlight.

+1
source

You can simply specify the absolute path, if the directory / directories do not exit, it will create the directory / directories.

 private void createDirectoryIfNeeded(String directoryName) { File theDir = new File(directoryName); if (!theDir.exists()) theDir.mkdirs(); } 
0
source
  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.. } 

You can also check if Boolean variable == True , but this check is better and more efficient.

0
source

Source: https://habr.com/ru/post/943977/