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

前端 未结 4 642
广开言路
广开言路 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 16:09

    From the documentation:

    Paths associated with the default provider are generally interoperable with the java.io.File class. Paths created by other providers are unlikely to be interoperable with the abstract path names represented by java.io.File. The toPath 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 the java.io.File object. In addition, the toFile method is useful to construct a File from the String representation of a Path.

    (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.

提交回复
热议问题