Java 中 File.exists() 的替代方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3833127/
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
Alternative to File.exists() in Java
提问by Ben
I never thought it would happen to me, but I ran into my first bug in Java:
我从没想过它会发生在我身上,但我在 Java 中遇到了第一个错误:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003595
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003595
I'm pretty much in the same exact situation as described in the bug (NFS on linux), and I'm seeing that File.exists() is not returning the correct value (at least not right away).
我几乎处于与错误(Linux 上的 NFS)中描述的完全相同的情况,并且我看到 File.exists() 没有返回正确的值(至少不是马上)。
So my question is, is there any alternative to this method of checking if a file exists? I'd prefer to keep it OS agnostic if possible.
所以我的问题是,这种检查文件是否存在的方法是否有任何替代方法?如果可能的话,我更愿意让它与操作系统无关。
EDIT: I have found a workaround. If you make a call to 'ls $filedir', the NFS refreshes whatever cache/metadata that is giving Java trouble, and File.exists() returns the correct value. Granted, this isn't totally ideal, since it hurts portability, but there are ways to deal with that problem.
编辑:我找到了一个解决方法。如果您调用 'ls $filedir',NFS 会刷新任何给 Java 带来麻烦的缓存/元数据,并且 File.exists() 返回正确的值。当然,这并不完全理想,因为它损害了可移植性,但是有办法解决这个问题。
Thanks, -Ben
谢谢,-本
采纳答案by Peter Lawrey
The basic problem you have with NFS is that it caches attributes, files and directories information. This means the information can be out of date. You might be able to turn off caching, you will see a significant reduction in performance.
NFS 的基本问题是它缓存属性、文件和目录信息。这意味着信息可能已经过时。您也许可以关闭缓存,您会看到性能显着降低。
The important thing to remember is that NFS is not a messaging service and is not designed for timely delivery of data.
要记住的重要一点是,NFS 不是消息传递服务,也不是为及时交付数据而设计的。
回答by Simon Dyson
I experienced the same problem and solved it with a call to file.getParentFile().list()
. Essentially the same as your solution, but OS agnostic.
我遇到了同样的问题并通过调用解决了它file.getParentFile().list()
。本质上与您的解决方案相同,但与操作系统无关。
回答by dty
What happens if File.exists()
returns true, then someone deletes the file/your NFS mount goes away, then you try and open the file? Basically, File.exists()
is useless since you need to handle the exceptions that can arise from opening the file anyway.
如果File.exists()
返回 true,然后有人删除文件/您的 NFS 挂载消失,然后您尝试打开文件,会发生什么情况?基本上,File.exists()
是无用的,因为您无论如何都需要处理打开文件时可能出现的异常。
回答by Ben Voigt
All File.exists
tells you is whether the file existed at some point in the past. It doesn't tell you:
一切都会File.exists
告诉您该文件在过去的某个时间点是否存在。它不会告诉你:
- Whether it will exist when you try to open it
- Whether you have permission to open it
- Anything useful at all, really
- 当您尝试打开它时它是否会存在
- 是否有权限打开
- 任何有用的东西,真的
So try to design your application so it can handle files that don't exist without trying to check for that in advance. (You'll have to handle various exceptions when actually working with the file.)
因此,请尝试设计您的应用程序,使其可以处理不存在的文件,而无需事先尝试检查。(在实际处理文件时,您必须处理各种异常。)
回答by Steve Emmerson
I notice that Java 7's java.nio.file.Path.exists() method returns false if the file doesn't exist or it's existence can't be determined. It would seem, therefore, that false negatives will be around for a while and that your code will need to tolerate them.
我注意到 Java 7 的 java.nio.file.Path.exists() 方法返回 false 如果文件不存在或者它的存在不能被确定。因此,假阴性似乎会持续一段时间,您的代码需要容忍它们。
回答by Adam
The obvious alternative is File.isFile(). Try that first.
显而易见的替代方法是 File.isFile()。先试试。
Although it will become inaccurate when reading read-only files you could always use the File.canWrite() to check if the file exists.
尽管在读取只读文件时会变得不准确,但您始终可以使用 File.canWrite() 来检查文件是否存在。
If both the above fail you could use the File.length(). If it returns 0L you know that the file does not exists.
如果以上都失败了,你可以使用 File.length()。如果它返回 0L,您就知道该文件不存在。