Java File.exists() 当文件存在时返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/919918/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:04:44  来源:igfitidea点击:

File.exists() returns false when file exists

javafile-io

提问by atsjoo

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 file = new File("utilities/data/someTextFile.txt");

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.

然后我做file.exists(),它返回false(!?)。如果未找到该文件,我将登录f.getAbsolutePath()到一个文件。当我查看路径时,似乎还可以。我可以将完整路径复制粘贴到 Windows 中的“运行”窗口中,文件打开正常。

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.

这似乎只发生在某些情况下。我可以随时重现故障,但我确信文件对象的路径不会因我为重现故障所做的操作而改变。

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

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

回答by Tom Hawtin - tackline

If the process does not have permissions to tell whether a file exists it will return false. It may be possible to open a file, but not tell by normal methods if it exists.

如果进程没有权限判断文件是否存在,它将返回 false。可能可以打开文件,但无法通过常规方法判断它是否存在。

回答by Kjetil Joergensen

If the situations where it fails involves running it as another user, and you're on Windows Vista/Windows 7, it could be caused by VirtualStore, the mechanism where Windows let an unprivileged user "write" places it normally cannot. The changes are however stored in "%USERPROFILE%\AppData\Local\VirtualStore\" which are private to each user account.

如果失败的情况涉及以另一个用户身份运行它,并且您使用的是 Windows Vista/Windows 7,则可能是由 VirtualStore 引起的,Windows 让非特权用户“写入”它通常无法放置的机制。但是,更改存储在每个用户帐户私有的“%USERPROFILE%\AppData\Local\VirtualStore\”中。

回答by Roman Zenka

I am seeing the following situation on Windows 7:

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

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

The file in question is "var\log", 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.

有问题的文件是“var\log”,绝对路径确实指的是普通子目录(不是虚拟存储)中的现有文件。这是从 IDE 中看到的。

回答by metoo

When ["Hide extensions for known file types."] is checked windows open "t.txt.txt" when type "t.txt" in [explorer]/[run windows] but programmatically not.

当[“隐藏已知文件类型的扩展名。”] 被选中时,当在 [资源管理器]/[运行窗口] 中键入“t.txt”但以编程方式不是时,窗口会打开“t.txt.txt”。

回答by Garima Bathla

It seems like there is a difference on how the path is specified in Java.

似乎在 Java 中指定路径的方式有所不同。

For example, if the file path is specified as file:/C:/DEV/test.txtthen

例如,如果文件路径指定为file:/C:/DEV/test.txtthen

File f = new File(filename);
f.exists();

will return false. The path might work in the explorer or in the browser, but it is a URL and not absolute file path.

会回来false。该路径可能在资源管理器或浏览器中有效,但它是一个 URL 而不是绝对文件路径。

But on the other hand if the file path is specified as C:/DEV/test.txtthen

但在另一方面,如果文件路径被指定为C:/DEV/test.txt

File f = new File(filename);
f.exists();

will return truebecause the path is not a URL, but it is a absolute path.

将返回,true因为路径不是 URL,而是绝对路径。

With Spring Frameworkthat is exactly what ResourceUtils.getFile(filename)does - where name can be either a URL or the absolute file path.

使用Spring Framework,这正是ResourceUtils.getFile(filename)它的作用 - 其中 name 可以是 URL 或绝对文件路径。

回答by Karl Lew

The above answers didn't help out in my case. As stated above, I had:

以上答案对我的情况没有帮助。如上所述,我有:

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

The root cause for this was that the Windows 7machine owner had modified the registry for CMD so that it would autorun a command to launch in a specific directory to work with Python. This modification crippled the Java 1.6code which apparently uses CMDon Windowsfor certain file operations, such as exists(). Eliminating the autorun from the registry solved the issue.

造成这种情况的根本原因是Windows 7机器所有者修改了 CMD 的注册表,以便它可以自动运行命令在特定目录中启动以使用 Python。此修改削弱了Java 1.6代码,该代码显然在Windows上使用CMD进行某些文件操作,例如. 从注册表中消除自动运行解决了这个问题。exists()

回答by Code Carpenter

Good responses everyone. I've found this seems to be a problem with Java accessing the root C:directory on Windows. Any other directory should be fine, but for some reason, specifically mentioning C:\or C:or C:/might give an error. I have resolved this very similar problem by trapping mention to new File("C:");and replacing it with new File(System.getProperty("file.separator"));or you should be able to hard code "\" instead of saying "c:" as your file directory and it might work out. Not elegant, but got the job done for me on this project.

大家反响好。我发现这似乎是 Java 访问C:Windows根目录的问题。任何其他目录应该罚款,但由于某些原因,特别提到C:\C:C:/会给出错误。我已经解决了这个非常相似的问题,通过捕获提及new File("C:");并用 new 替换它,File(System.getProperty("file.separator"));或者你应该能够硬编码“\”而不是说“c:”作为你的文件目录,它可能会解决。不优雅,但在这个项目上为我完成了工作。

I hope it helps. Might not be the right solution, but at least it worked for me. I'm on JRE 1.6, Win 7. Cheers!

我希望它有帮助。可能不是正确的解决方案,但至少它对我有用。我在JRE 1.6, Win 7。干杯!

Respectfully,

尊敬,

@Carpenter1010

@木匠1010

回答by R1234

The new Filecommand just creates an instance of a file using the given path name. It doesn't actually create a file on the hard drive.

new File命令仅使用给定的路径名​​创建文件的实例。它实际上并没有在硬盘驱动器上创建文件。

If you say

如果你说

File file = new File ("path");
file.exists() 

This can return true only if there was an existing file with the same path. If you intended to check for the same file declared in the first line, you may need to use it this way.

仅当存在具有相同路径的现有文件时,这才能返回 true。如果您打算检查第一行中声明的同一个文件,您可能需要以这种方式使用它。

File file = new File ("path");
file.createNewFile();
file.exists();

Now this will return true.

现在这将返回 true。

回答by petehern

Obviously there are a number of possible causes and the previous answers document them well, but here's how I solved this for in one particular case:

显然有许多可能的原因,之前的答案很好地记录了它们,但这是我在一个特定情况下解决这个问题的方法:

A student of mine had this problem and I nearly tore my hair out trying to figure it out. It turned out that the file didn't exist, even though it looked like it did. The problem was that Windows 7 was configured to "Hide file extensions for known file types." This means that if file appears to have the name "data.txt" its actual filename is "data.txt.txt".

我的一个学生遇到了这个问题,我差点把头发扯掉试图弄清楚。结果发现该文件不存在,即使它看起来确实存在。问题在于 Windows 7 被配置为“隐藏已知文件类型的文件扩展名”。这意味着如果文件似乎具有名称“data.txt”,则其实际文件名是“data.txt.txt”。

Hope this helps others save themselves some hair.

希望这可以帮助其他人节省一些头发。

回答by Hussein Maziad

I think you should use backslash instead , like this:

我认为您应该改用反斜杠,如下所示:

File file = new File("C:\\User\\utilities\\data\\someTextFile.txt"); (two backslashes , not a typo )

File file = new File("C:\\User\\utilities\\data\\someTextFile.txt"); (两个反斜杠,不是打字错误)

Should solve the problem :)

应该可以解决问题:)