can File.exists can return false if the file exists?
Bernd Eckenfels
ecki at zusammenkunft.net
Thu Apr 4 20:22:53 UTC 2019
Yes it can and does, the getBooleanAttributes method of the filesystem providers cannot communicate IOExceptions, so it will have to return unset flags for this’s case.
(Also there are some OS specific conditions where the file stat might be stale, especially if it is a networked filesystem).
The missing possibility for returning IOExceptions on File IO was one of the drivers for NIO APIs (unfortunately the alternative Files.exist(Path) took no advantage of this)
What you can do is to open the file and inspect the exception and/or use Files.walkFileTree to iterate the parent (as it gives you basicfileattributes view based on the directory read)
.
Gruss
Bernd
--
http://bernd.eckenfels.net
________________________________
Von: core-libs-dev <core-libs-dev-bounces at openjdk.java.net> im Auftrag von Sean Bridges <sean.bridges at gmail.com>
Gesendet: Donnerstag, April 4, 2019 9:45 PM
An: core-libs-dev at openjdk.java.net
Betreff: can File.exists can return false if the file exists?
Hey,
Is it possible for File.exists(...) to return false even if the file
exists.
The java docs for File.exists say,
"return true if and only if the file or directory denoted by this abstract
pathname exists; false otherwise"
Looking at the implementation, the method does,
s.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0
But FileSystem.getBooleanAttributes says,
"Return the simple boolean attributes for the file or directory denoted
by the given abstract pathname, *or zero if it does not exist or some*
* other I/O error occurs*."
So it seems that File.exists can return false for files which exist.
I think we ran into this on a linux server which reached the limit on open
file handles, and File#exists returned false even though the file existed.
Is there a way to reliably tell in java if a files exists. Even a method
which returned true/false/unknown would be preferable.
Thanks,
Sean
More information about the core-libs-dev
mailing list