Java File.exists() 与 File.isFile()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20523247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java File.exists() versus File.isFile()
提问by jaco0646
I'm unable to think of a realistic use case for the method 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.).
我无法想到java.io.File.exists()
Java 7 中该方法或其等效物的实际用例java.nio.file.Files.exists(Path)
。看来,isFile()
或isDirectory()
会在所有情况下最好(或canRead()
,canWrite()
等等)。
For example, in How do I check if a file exists in Java?, the accepted answer seems silly, as the second answer points out.
例如,在如何检查 Java 中是否存在文件?,正如第二个答案所指出的那样,接受的答案似乎很愚蠢。
Can anyone give an example where it's useful to know that a thing exists, withoutknowing whether the thing is a file or directory?
任何人都可以举一个例子,在不知道事物是文件还是目录的情况下,知道事物存在是有用的吗?
EDIT: I understand what File.exists()
does. My question is, whenwould 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."
编辑:我明白是什么File.exists()
。我的问题是,该功能何时会帮助某人?我正在寻找这样的例子,“File.exists()
在 _ _ _ _ _ _ 时使用,因为在这种情况下既File.isFile()
不会也不File.isDirectory()
添加任何值。”
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,
回想起来,我认为我的困惑是关于File
类的 JavaDoc 中两个看似矛盾的陈述。第一句将类定义为,
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,
我认为第三个文件系统对象的例子会对文档有很大帮助;但该类别似乎连名称都没有,导致 JavaDoc 对该Files
类的措辞笨拙:静态方法的集合,
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.
在接受的答案中,@koral 将这些其他类型称为“特殊文件”。这对我来说似乎很合适。它们是如此特别,我不知道它们存在。
采纳答案by koral
Answering to the last question of @jaco0646:
回答@jaco0646的最后一个问题:
Use File.exists()
when dealing with special fileslike named pipes, sockets or device files.
File.exists()
在处理命名管道、套接字或设备文件等特殊文件时使用。
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.
这些都不是普通的文件,也没有目录,也没有符号链接这样既File.isFile()
和File.isDirectory()
返回false
时File.exists()
将返回true
。例如/dev/null
(在 Unix 兼容的操作系统上)是一个设备文件。
Theoretically there may beperformance differences visible when processing large amounts of files. This depends also on filesystem, JVM implementation details, OS etc.
理论上,处理大量文件时可能会出现性能差异。这也取决于文件系统、JVM 实现细节、操作系统等。
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()
.
例如。在 Android 上File.exists()
是使用access()
系统调用 while File.isFile()
/File.isDirectory()
use 来实现的stat()
。在这种情况下,处理stat()
输出在用户空间中需要比access()
.
回答by Ian McLaird
The only use case I can think of would be one for lock files, or something similar. A time where whether the file is a regular file or a directory is immaterial and the mere existence of something with that name is good enough to trigger program behavior.
我能想到的唯一用例是锁定文件或类似的用例。文件是常规文件还是目录无关紧要,并且仅存在具有该名称的内容就足以触发程序行为的时间。
Possibly also checking whether a cache exists. The cache could be implemented by different providers, some of which use a directory structure and some of which use a zip file (I'm making this up as I go, by the way -- I'm not saying this is a goodidea).
可能还会检查缓存是否存在。缓存可以由不同的提供者实现,其中一些使用目录结构,其中一些使用 zip 文件(顺便说一下,我正在制作它——我并不是说这是一个好主意)。
回答by JonAar Livernois
Well it's a convenience to be able to set LinkOptions in the parameters. The actual API for this call is
能够在参数中设置 LinkOptions 很方便。此调用的实际 API 是
static java.nio.file.Files.exists(Path path, LinkOption... options)
You might not want to follow symbolic links in the file path you specified. In this case, call Files.exists(path) - with link option NOFOLLOW_LINKS
, and you're set.
您可能不想遵循指定文件路径中的符号链接。在这种情况下,调用 Files.exists(path) - 和链接选项NOFOLLOW_LINKS
,你就设置好了。
It's also nice to have staticmethods when you don't really want to create more objects (like transient File objects) in your space than you need.
当您真的不想在您的空间中创建比您需要的更多的对象(如瞬态文件对象)时,拥有静态方法也很好。