Is it possible to get a Path object from a java.io.File
?
I know you can convert a path to a file using toFile()
method, but I couldn't find the opposite conversion. Is there a way to do this in Java 6 or lower?
Is it possible to get a Path object from a java.io.File
?
I know you can convert a path to a file using toFile()
method, but I couldn't find the opposite conversion. Is there a way to do this in Java 6 or lower?
Yes, you can get it from the
File
object by usingFile.toPath()
. Keep in mind that this is only for Java 7+. Java versions 6 and below do not have it.You likely want
File.toPath()
.From the documentation:
(emphasis mine)
So, for
toFile
:And
toPath
:As many have suggested, JRE v1.7 and above has File.toPath();
On Oracle's jdk 1.7 documentation which is also mentioned in other posts above, the following equivalent code is described in the description for toPath() method, which may work for JRE v1.6;