Java 中 File.exists() 的替代方法 [英] Alternative to File.exists() in Java

查看:29
本文介绍了Java 中 File.exists() 的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从没想过它会发生在我身上,但我在 Java 中遇到了第一个错误:

I never thought it would happen to me, but I ran into my first bug in Java:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003595

我几乎处于与错误(Linux 上的 NFS)中描述的完全相同的情况,并且我看到 File.exists() 没有返回正确的值(在至少不是马上).

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).

所以我的问题是,除了这种检查文件是否存在的方法之外,还有其他选择吗?如果可能,我更愿意让它与操作系统无关.

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.

我找到了一个解决方法.如果您调用 ls $filedir,NFS 会刷新任何给 Java 带来麻烦的缓存/元数据,并且 File.exists() 返回正确的值.诚然,这并不完全理想,因为它损害了便携性,但有办法解决这个问题.

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.

推荐答案

NFS 的基本问题是它缓存属性、文件和目录信息.这意味着信息可能已经过时.您或许可以关闭缓存,但性能会显着降低.

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.

这篇关于Java 中 File.exists() 的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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