How to check if a path points to an existing file with Java 7's new File API?

28,575

Solution 1

Use the Files class:

Files.exists(path);

EDIT: to answer your subsequent question, I think the reason that the method is in another class is that Path is an interface, and they wanted to provide an implementation (similar to putting sorting methods in the Collections class instead of the List interface).

Not directly related to the question, but as per ratchet freak there is an optional varags argument to the method as well, which determines how symbolic links are handled

Read the Javadocs from Oracle here.

Solution 2

look in the utility class Files for the package:

Files.exists(Path path,LinkOption... options)

Solution 3

In the new file system API then all file operations are defined by the Files class. Mostly these operations are implemented in terms of other operations or they delegate to the appropriate file system provider. The Path interface on the other hand is where the path operations are defined. A Path is just the object that is used to locate a file. If you want to do operations on a file then you invoke the appropriate Files method, specifying the Path to locate the file.

Share:
28,575
soc
Author by

soc

Updated on March 24, 2020

Comments

  • soc
    soc about 4 years

    The old, more or less deprecated java.io.File API had a method exists which returned true if the File pointed to an existing one in the file system, but I couldn't find any comparable method for java.nio.file.Path:

    scala> import java.nio.file._
    import java.nio.file._
    
    scala> val path = Paths.get("/foo")
    path: java.nio.file.Path = /foo
    
    scala> path.
    asInstanceOf     compareTo        endsWith         getFileName      getFileSystem    getName          getNameCount     
    getParent        getRoot          isAbsolute       isInstanceOf     iterator         normalize        register         
    relativize       resolve          resolveSibling   startsWith       subpath          toAbsolutePath   toFile           
    toRealPath       toString         toUri  
    

    Of course I could just convert the path back to a File but I guess there is a better way to do that.

    Edit: OK, thanks to everyone pointing out Files.exists. Does someone know why it got more complicated (than having a simple exists method on Path)?

  • soc
    soc almost 13 years
    Isn't that exactly what I said, mentioning that there has to be a better way? :-)
  • Femi
    Femi almost 13 years
    Ah. So you're objecting to having to step through that. Well, you could try using toRealPath() which I believe will throw an IOException if the file doesn't exist?
  • soc
    soc almost 13 years
    So basically it makes sure there is only one implementation (supplied by Oracle) of the methods in Files and not potentially several different, which would have been possible if it was defined in Path?
  • OpenSauce
    OpenSauce almost 13 years
    Yes, I imagine that's the reason. Only one implementation, and one that's independent of the Path implementation.
  • soc
    soc almost 13 years
    I guess it will only be a matter of weeks until Scala people have added the methods back to the more convenient place :-)
  • Vojta
    Vojta almost 8 years
    or Files.isRegularFile(path) if you do not want accept directories
  • Andrew S
    Andrew S over 6 years
    The new NIO also handles paths inside zip files. For this you get an unsupported operation exception.