在Java中替代File.exists() [英] Alternative to File.exists() in Java

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

问题描述

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

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



我几乎和bug(NFS on linux)中描述的一样,我看到File.exists()没有返回正确的值(至少不是马上)。

所以我的问题是,有没有其他的方法来检查文件是否存在?如果可能的话,我宁愿保持操作系统不可知。



编辑:我找到了一个解决方法。如果你打电话给'ls $ filedir',NFS刷新给Java提供的任何缓存/元数据,File.exists()返回正确的值。诚然,这不是完全理想的,因为它伤害了可移植性,但有办法解决这个问题。

感谢,
- 本

解决方案

您使用NFS的基本问题是缓存属性,文件和目录信息。这意味着信息可能会过时。您可能能够关闭缓存,您将看到性能显着下降。



重要的是要记住,NFS不是一个消息服务,并没有设计以便及时交付数据。

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

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.

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.

Thanks, -Ben

解决方案

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.

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天全站免登陆