Get java.nio.file.Path object from java.io.File

前端 未结 4 769
广开言路
广开言路 2020-12-23 15:36

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 fin

4条回答
  •  执念已碎
    2020-12-23 15:59

    As many have suggested, JRE v1.7 and above has File.toPath();

    File yourFile = ...;
    Path yourPath = yourFile.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;

    File yourFile = ...;
    Path yourPath = FileSystems.getDefault().getPath(yourFile.getPath());
    

提交回复
热议问题