Java File.exists()与File.isFile() [英] Java File.exists() versus File.isFile()

查看:256
本文介绍了Java File.exists()与File.isFile()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法想到方法如何检查文件是否在Java中存在?,第二个答案指出,接受的答案似乎很愚蠢.

任何人都可以举一个例子,知道某个事物是否存在是有用的,而无需知道该事物是文件还是目录?

我了解File.exists()的作用.我的问题是,何时该功能可以帮助某人?我正在搜索一个示例,例如在_ _ _ _ _ _时使用File.exists(),因为在这种情况下File.isFile()File.isDirectory()都不会添加任何值."


回想一下,我认为我的困惑是关于File类的JavaDoc中的两个看似矛盾的语句.第一句话将类定义为:

文件和目录路径名的抽象表示.

这听起来像是明显的二分法;但在更远的地方,文档会用它来对付它,

此类的实例可能会或可能不会表示实际的文件系统对象,例如文件或目录.

我认为第三个文件系统对象的示例将在文档中提供极大帮助;但是该类别似乎甚至都没有名称,导致Files类的JavaDoc措辞尴尬:静态方法的集合,

在文件,目录或其他类型的文件上运行的

.

在接受的答案中,@ koral将这些其他类型称为特殊文件".这似乎很适合我.它们是如此特别,我不知道它们的存在.

解决方案

回答@ jaco0646的最后一个问题:

在处理特殊文件(如命名管道,套接字或设备文件)时使用File.exists() .

这些文件不是常规文件,目录或符号链接,因此File.isFile()File.isDirectory()都将返回false,而File.exists()将返回true.例如,/dev/null(在Unix兼容操作系统上)是设备文件.

理论上,在处理大量文件时,可能会出现 性能差异.这还取决于文件系统,JVM实现细节,操作系统等.

例如.在Android File.exists() 是通过 access() 系统调用实现的,而 File.isFile() /File.isDirectory() 使用 stat() .在这种情况下,处理> c23> 输出比java.io.File.exists() or its equivalent in Java 7 java.nio.file.Files.exists(Path). It seems that isFile() or isDirectory() would be preferable in all cases (or canRead(), canWrite(), etc.).

For example, in How do I check if a file exists in Java?, the accepted answer seems silly, as the second answer points out.

Can anyone give an example where it's useful to know that a thing exists, without knowing whether the thing is a file or directory?

EDIT: I understand what File.exists() does. My question is, when would that functionality ever help someone? I'm searching for an example like, "Use File.exists() when _ _ _ _ _ _, because neither File.isFile() nor File.isDirectory() add any value in that case."


In retrospect, I think my confusion here was regarding two seemingly contradictory statements in the JavaDoc of the File class. The first sentence defines the class as,

An abstract representation of file and directory pathnames.

That sounds like a clear dichotomy; but further in, the doc counters it with,

Instances of this class may or may not denote an actual file-system object such as a file or a directory.

I think an example of a third file-system object would have helped immensely in the documentation; but that category seems to lack even a name, resulting in the awkward phrasing of the JavaDoc for the Files class: a collection of static methods,

that operate on files, directories, or other types of files.

In the accepted answer, @koral refers to these other types as "special files". That seems apt to me. They are so special, I didn't know they existed.

解决方案

Answering to the last question of @jaco0646:

Use File.exists() when dealing with special files like named pipes, sockets or device files.

Those are not regular files nor directories nor symlinks so both File.isFile() and File.isDirectory() will return false while File.exists() will return true. For example /dev/null (on Unix compatible OSes) is a device file.

Theoretically there may be performance differences visible when processing large amounts of files. This depends also on filesystem, JVM implementation details, OS etc.

Eg. on Android File.exists() is implemented using access() system call while File.isFile()/File.isDirectory() use stat(). In this case processing stat() output requires more logic in userspace than access().

这篇关于Java File.exists()与File.isFile()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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