为什么'File.exists'返回true,即使NIO'Files'类中的'Files.exists'返回false [英] Why does 'File.exists' return true, even though 'Files.exists' in the NIO 'Files' class returns false

查看:155
本文介绍了为什么'File.exists'返回true,即使NIO'Files'类中的'Files.exists'返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定网络文件夹中是否存在文件:

I am trying to determine if a file exists in a network folder:

// File name is "\\QWERTY\folder\dir\A123456.TXT"
Path path = Paths.get("\\\\QWERTY\\folder\\dir\\A123456.TXT")

使用NIO 文件

Files.exists(path) == false

使用文件

path.toFile().exists() == true

使用根据我们的测试,文件似乎是正确的。为什么文件的工作效果优于文件

Using File seems to be the correct one according to our tests. Why does File work better than Files?

所以,这是什么?不能两者兼得!

So, which is it? Can't be both!

但等等,还有 Files.notExists(路径)

当网络共享文件确实存在时

Files.exists(path): false
Files.notExists(path): false
path.toFile().exists(): true

当网络共享文件确实

When the network share file actually does not exist

Files.exists(path): false
Files.notExists(path): true
path.toFile().exists(): false

查看上述三个结果的另一种同样疯狂的方式

boolean exists = Files.exists(path) || !Files.notExists(path)
boolean notExists = Files.notExists(path) || !Files.exists(path)
boolean oldFashionedExists = path.toFile().exists()

:smileyFace:

:smileyFace:

环境和评论

该程序正在运行Windows 8.1 Pro 32位计算机(操作系统和计算机)并从Windows 2008 R2(32位)计算机检查网络共享。

The program is running on a Windows 8.1 Pro 32 bit machine (OS and machine) and checking on a network share from a Windows 2008 R2 (32 bit) machine.

确定Files.exists失败了,我安装了WatchService来监视文件夹,并在Files.exists检查时看到该文件确实存在。然后我记录了两个方法,发现File.exists是正确的。

To determine that Files.exists was failed, I installed a WatchService to monitor the folder and saw that the file did exist when Files.exists was checking. I then logged as both methods and found File.exists to be the correct one.

现在,在我的代码中,我将检查为文件。存在(路径)|| path.toFile()。exists()

Now, in my code I have the check as Files.exists(path) || path.toFile().exists().

有点似乎很蠢。可能只是稍后逃脱。只是试图让工程师在甲骨文的怀疑中受益,但整个事情相当愚蠢,他们报告的不同。

Kinda seems stupid to have to do both. Probably could just get away with the later. Just trying to give the engineers over at Oracle the benefit of the doubt, but the whole thing is rather silly that they report different.

另外,我不在乎是否'存在'立即过时。我只是想知道文件是否存在于我们正在检查的瞬间。我从来没有遇到过这种情况 - 我和我的另一位开发人员花了30个小时试图弄清楚为什么我们的程序没有因为这个'功能'而接口。

Also, I don't care if 'exists' is immediately outdated. I just want to know if the file exists at the instant that we are checking. I've never come across this -- we just spent 30 hours between me and another developer trying to figure out why our programs are not interfacing because of this 'feature'.

冥想一段时间


File.exists():返回true if并且仅当此抽象路径名表示的文件或目录存在时 ;否则为false。

File.exists(): Returns true if and only if the file or directory denoted by this abstract pathname exists; false otherwise.

Files.exists():如果文件存在,则返回true;否则返回false。如果文件不存在,则为false 或无法确定其存在。

Files.exists(): Returns true if the file exists; false if the file does not exist or its existence cannot be determined.

这让我感到震惊! 当且仅当此抽象路径名表示的文件或目录存在时;否则为false与如果文件存在则为;如果文件不存在或无法确定其存在,则为

That cracks me up! "if and only if the file or directory denoted by this abstract pathname exists; false otherwise" is at odds with "true if the file exists; false if the file does not exist or its existence cannot be determined"

那么,如果无法确定存在, File.exists 如何才能成立?显然,存在可以(并且正在)由文件确定,但不是由文件确定。

So, how still can File.exists be true if "the existence cannot be determined"? Obviously, the existence can be (and is being) determined by File but not by Files.

推荐答案

至于为何可能存在两者之间的差异,对比他们的文档:

As to why there may be a difference between the two, contrast their documentation:


File.exists():当且仅当表示的文件或目录时返回true这个抽象路径名存在;否则为false。

File.exists(): Returns true if and only if the file or directory denoted by this abstract pathname exists; false otherwise.

Files.exists():如果文件存在,则返回true;否则返回false。如果文件不存在,则为false 或无法确定其存在。

Files.exists(): Returns true if the file exists; false if the file does not exist or its existence cannot be determined.

那可能可能解释两者之间的差异,也许是文件一个人在查明文件的存在时遇到了麻烦。

That could possibly explain the difference between the two, perhaps the Files one is having troubles ascertaining the existence of the file.

例如,在Linux下,可以设置目录和文件权限,以便您可以打开存在但不能查看存在的文件(通过取消文件所在目录的读取权限,同时保持文件权限更加开放)。

For example, under Linux, it's possible to set up directory and file permissions in such a way that you can open a file that exists but cannot see that it exists (by taking away read permission on the directory the file is in while leaving the file permissions more open).

根据更多Oracle文档 Files.exists()如果验证文件存在,则返回 true

As per more of Oracle's documentation, Files.exists() only returns true if the file is verified to exist.

返回值 false 表示它不存在。

A return value of false does not mean it doesn't exist.

他们建议你同时使用( ) notExists()以涵盖三种可能性,例如:

They suggest you use both exists() and notExists() to cover the three possibilities, something like:

if (Files.exists(fspec)) {
    System.out.println("It exists!");
else if (Files.notExists(fspec)) {
    System.out.println("It does not exist!");
else
    System.out.println("I have no idea!");

这涵盖了上述链接中涵盖的文件状态的三种可能性:

That covers the three possibilities of file state covered in that link above:


  • 验证文件存在。

  • 验证文件不存在。

  • 文件的状态未知。当程序无权访问该文件时,可能会出现此结果。

这篇关于为什么'File.exists'返回true,即使NIO'Files'类中的'Files.exists'返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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