Get java.nio.file.Path object from java.io.File
You likely want File.toPath()
.
Yes, you can get it from the File
object by using File.toPath()
. Keep in mind that this is only for Java 7+. Java versions 6 and below do not have it.
From the documentation:
Paths associated with the default
provider
are generally interoperable with thejava.io.File
class. Paths created by other providers are unlikely to be interoperable with the abstract path names represented byjava.io.File
. ThetoPath
method may be used to obtain a Path from the abstract path name represented by a java.io.File object. The resulting Path can be used to operate on the same file as thejava.io.File
object. In addition, thetoFile
method is useful to construct aFile
from theString
representation of aPath
.
(emphasis mine)
So, for toFile
:
Returns a
File
object representing this path.
And toPath
:
Returns a
java.nio.file.Path
object constructed from the this abstract path.