如何使用 Java 7 的新文件 API 检查路径是否指向现有文件?
旧的、或多或少已弃用的 java.io.File
API 有一个方法 exists
,如果 File
指向现有的一个,则该方法返回 true文件系统,但我找不到 java.nio.file.Path 的任何类似方法:
scala> import java.nio.file._
import java.nio.file._
scala> val path = Paths.get("/foo")
path: java.nio.file.Path = /foo
scala> path.
asInstanceOf compareTo endsWith getFileName getFileSystem getName getNameCount
getParent getRoot isAbsolute isInstanceOf iterator normalize register
relativize resolve resolveSibling startsWith subpath toAbsolutePath toFile
toRealPath toString toUri
当然,我可以将 path
转换回 File
但我想有更好的方法来做到这一点。
编辑:好的,感谢大家指出 Files.exists
。有人知道为什么它变得更复杂(比在 Path
上有一个简单的 exists
方法)吗?
The old, more or less deprecated java.io.File
API had a method exists
which returned true if the File
pointed to an existing one in the file system, but I couldn't find any comparable method for java.nio.file.Path
:
scala> import java.nio.file._
import java.nio.file._
scala> val path = Paths.get("/foo")
path: java.nio.file.Path = /foo
scala> path.
asInstanceOf compareTo endsWith getFileName getFileSystem getName getNameCount
getParent getRoot isAbsolute isInstanceOf iterator normalize register
relativize resolve resolveSibling startsWith subpath toAbsolutePath toFile
toRealPath toString toUri
Of course I could just convert the path
back to a File
but I guess there is a better way to do that.
Edit: OK, thanks to everyone pointing out Files.exists
. Does someone know why it got more complicated (than having a simple exists
method on Path
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
Files
类:Files.exists(path);
编辑:为了回答您的后续问题,我认为该方法位于另一个类中的原因是
Path
是一个接口,他们希望提供一个实现(类似于将排序方法放在Collections
类中而不是List
接口中)。与问题没有直接关系,但根据棘轮怪胎,该方法还有一个可选的 varags 参数,它决定如何处理符号链接
从 Oracle 读取 Javadocs在这里。
Use the
Files
class:Files.exists(path);
EDIT: to answer your subsequent question, I think the reason that the method is in another class is that
Path
is an interface, and they wanted to provide an implementation (similar to putting sorting methods in theCollections
class instead of theList
interface).Not directly related to the question, but as per ratchet freak there is an optional varags argument to the method as well, which determines how symbolic links are handled
Read the Javadocs from Oracle here.
在实用程序类 Files 中查找包装:
look in the utility class Files for the package:
在新的文件系统 API 中,所有文件操作均由 Files 类定义。大多数情况下,这些操作是根据其他操作来实现的,或者委托给适当的文件系统提供者。另一方面,Path 接口是定义路径操作的地方。路径只是用于定位文件的对象。如果要对文件执行操作,则调用适当的 Files 方法,指定路径来定位该文件。
In the new file system API then all file operations are defined by the Files class. Mostly these operations are implemented in terms of other operations or they delegate to the appropriate file system provider. The Path interface on the other hand is where the path operations are defined. A Path is just the object that is used to locate a file. If you want to do operations on a file then you invoke the appropriate Files method, specifying the Path to locate the file.
无需为此添加新方法:使用
Path.toFile()
获取文件,然后对其运行exists
。No need to add a new method for that: use
Path.toFile()
to get a file, then runexists
on that.