How to check if a path points to an existing file with Java 7's new File API?
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.
soc
Updated on March 24, 2020Comments
-
soc about 4 years
The old, more or less deprecated
java.io.File
API had a methodexists
which returned true if theFile
pointed to an existing one in the file system, but I couldn't find any comparable method forjava.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 aFile
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 simpleexists
method onPath
)? -
soc almost 13 yearsIsn't that exactly what I said, mentioning that there has to be a better way? :-)
-
Femi almost 13 yearsAh. 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 almost 13 yearsSo 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 inPath
? -
OpenSauce almost 13 yearsYes, I imagine that's the reason. Only one implementation, and one that's independent of the
Path
implementation. -
soc almost 13 yearsI guess it will only be a matter of weeks until Scala people have added the methods back to the more convenient place :-)
-
Vojta almost 8 yearsor
Files.isRegularFile(path)
if you do not want accept directories -
Andrew S over 6 yearsThe new NIO also handles paths inside zip files. For this you get an unsupported operation exception.