Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.nio.file.Files class

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exists
public static boolean exists(Path path,
LinkOption... options)

Tests whether a file exists.

The options parameter may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. By default, symbolic links are followed. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.

Note that the result of this method is immediately outdated. If this method indicates the file exists then there is no guarantee that a subsequence access will succeed. Care should be taken when using this method in security sensitive applications.



notExists

public static boolean notExists(Path path,
LinkOption... options)

Tests whether the file located by this path does not exist. This method is intended for cases where it is required to take action when it can be confirmed that a file does not exist.

The options parameter may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. By default, symbolic links are followed. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.

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. As with the exists method, the result of this method is immediately outdated. If this method indicates the file does exist then there is no guarantee that a subsequence attempt to create the file will succeed. Care should be taken when using this method in security sensitive applications.

Please help, i couldn't figure out what is difference between them.

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

if exist() can return false then what is need of notExists() ?
 
Tony Docherty
Bartender
Posts: 2942
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is down to what happens if the existence of a file cannot be determined, in such a case they both return false ie:
exists() only returns true if the file definitely exists at that moment in time, notExists() only returns true if the file definitely does not exist at the that moment in time.

So if you were to do if ( !Files.exists() ) the if code block would execute if the file definitely did not exist OR the system could not determine if the file existed or not, whereas if you did if ( notExists() ) the if code block would only execute if the file definitely did not exist.
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic