File.exists() 当文件存在时返回 false [英] File.exists() returns false when file exists

查看:154
本文介绍了File.exists() 当文件存在时返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个错误,我似乎找不到任何背后的逻辑.我有这个 File 对象,它是这样创建的:

I've encountered a bug I can't seem to find any logic behind. I have this File object, which is created like this:

File file = new File("utilities/data/someTextFile.txt");

然后我执行file.exists(),它返回false (!?).如果找不到该文件,我会将 f.getAbsolutePath() 记录到文件中.当我查看路径时,似乎还可以.我可以将完整路径复制粘贴到 Windows 的运行"窗口中,并且文件打开正常.

I then do file.exists(), and it returns false (!?). If the file is not found, I'm logging f.getAbsolutePath() to a file. When I look at the path, it seems OK. I can copy-paste the complete path into the "Run"-window in Windows and the file opens fine.

该文件始终存在,并且在我的应用程序运行期间不会被删除或更改.它位于本地机器上.

The file exists at all times and is not deleted nor changed during the running of my application. It is located at the local machine.

这似乎只发生在某些情况下.我可以随时重现该错误,但我确信我为重现该错误所做的操作不会更改文件对象的路径.

This only seems to occur in certain situations. I can reproduce the fault at any time, but I'm sure the path of the file object is not changed by the actions I make to reproduce the fault.

什么会导致 file.exists() 返回 false?这是否与权限或文件锁定等有关?

What can cause file.exists() to return false? Does this have something to do with permissions or file locks, etc.?

推荐答案

我在 Windows 7 上看到以下情况:

I am seeing the following situation on Windows 7:

file.exists() == false
file.getAbsoluteFile().exists() == true

有问题的文件是varlog",绝对路径确实指的是普通子目录(不是虚拟存储)中的现有文件.这是从 IDE 中看到的.

The file in question is "varlog", the absolute path does refer to an existing file that is in a normal subdirectory (not a virtual store). This is seen from the IDE.

这篇关于File.exists() 当文件存在时返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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