File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes java.nio.file.Files class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "java.nio.file.Files class" Watch "java.nio.file.Files class" New topic
Author

java.nio.file.Files class

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

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() ?


My one regret in life is that I am not someone else.
- Woody Allen
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2250
    
  47
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

Joined: Dec 05, 2012
Posts: 305

Thank you
 
Consider Paul's rocket mass heater.
 
subject: java.nio.file.Files class