[ https://issues.apache.org/jira/browse/IO-340?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13428589#comment-13428589 ]
Sebb commented on IO-340: ------------------------- Strange that file.exists() should not cause a handle refresh whereas it appears file.length() does. But it's not a full solution as file.length() can be zero for a file that does exist. Perhaps it works for you because none of your files are empty? Might be better to check for lastModified != 0, as that would potentially work with empty files too. Nevertheless, all code should still allow for the fact that files can appear/disappear between the check and the next file operation. Potentially even on local storage if a hardware fault occurs. > The use of file.exists() is failure prone. Where file.exists() is checked, it > should be replaced with the following dual check: exists = ( file.exists() > || file.length()>0 ) > ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ > > Key: IO-340 > URL: https://issues.apache.org/jira/browse/IO-340 > Project: Commons IO > Issue Type: Bug > Components: Utilities > Affects Versions: 2.5, 3.x > Environment: All > Reporter: Wilf Middleton > Labels: File, exists, false > Fix For: 2.5, 3.x > > Original Estimate: 24h > Remaining Estimate: 24h > > The use of file.exists() is failure prone. In many cases the jvm can report > that a file does not exist even when it does. This is due to the caching in > the operating system. > Since file.exists() is prone to report false values, a second check can and > should be made to file.length()>0. > Where file.exists() is checked, it should be replaced with the following dual > check: > A file more reliably exists if: file.exists() || file.length()>0 > Then if the file does not exist, file.length will be 0. -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira