将对象从java.nio.file.Path转换为java.io.File [英] Convert object from java.nio.file.Path to java.io.File

查看:81
本文介绍了将对象从java.nio.file.Path转换为java.io.File的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能将定义为 java.nio.file.Path 的对象转换为 java.io.File

I would like to know if it is possible somehow to convert an object that is defined as java.nio.file.Path to java.io.File

推荐答案

java.nio.file.Path java.io.File 类都提供了一种方法从一个传递到另一个.

Both java.nio.file.Path and java.io.File classes provides a way to pass from the one to the other.

1)在 Path 对象上调用 toFile()返回代表它的 File .

1) Invoking toFile() on a Path object returns a File representing it.

Path.toFile() javadoc:

Path.toFile() javadoc :

返回表示此路径的 File 对象.此 Path 所在的位置与默认提供程序关联,则此方法等效返回使用 String 表示构造的 File 对象的路径.

Returns a File object representing this path. Where this Path is associated with the default provider, then this method is equivalent to returning a File object constructed with the String representation of this path.

如果此路径是通过调用 File toPath 方法创建的,则存在不能保证此方法返回的 File 对象是否相等到原始文件.

If this path was created by invoking the File toPath method then there is no guarantee that the File object returned by this method is equal to the original File.

2)相反,在 File 对象上调用 toPath()会返回表示它的 Path .

2) Reversely, invoking toPath() on a File object returns a Path representing it.

File.toPath() javadoc:

File.toPath() javadoc :

返回从此摘要构造的 java.nio.file.Path 对象小路.生成的 Path 与默认文件系统关联.

Returns a java.nio.file.Path object constructed from the this abstract path. The resulting Path is associated with the default-filesystem.

第一次调用此方法就像调用它一样等同于计算表达式:

The first invocation of this method works as if invoking it were equivalent to evaluating the expression:

FileSystems.getDefault().getPath(this.getPath());

此方法的后续调用将返回相同的 Path .

Subsequent invocations of this method return the same Path.

这篇关于将对象从java.nio.file.Path转换为java.io.File的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆