...
Prior to the Java SE 7 release, the
java.io.File
class was the mechanism used for file I/O, but it had several drawbacks.Many methods didn't throw exceptions when they failed, so it was impossible to obtain a useful error message. For example, if a file deletion failed, the program would receive a "delete fail" but wouldn't know if it was because the file didn't exist, the user didn't have permissions, or there was some other problem.
Consequently, it is easy for file operations to can silently fail silently, if the methods' return values are ignored. Therefore, do not ignore Java programs must check the return values of file-based methods. (This rule method that perform file I/O (this is a specific instance of rule EXP00-J. Do not ignore values returned by methods.)
...
This noncompliant code example attempts to delete the a specified file specified, but gives no indication of its success. The [API 2006] defines requires File.delete()
to only throw a SecurityException
, if the program is not authorized to delete the file. No other exceptions are thrown; so it is easy for the deletion to fail, with no indication of whycan silently fail.
Code Block | ||
---|---|---|
| ||
File file = /* initialize */ file.delete(); |
...
Risk Assessment
Failure to check file operation errors the return values of methods that perform file I/O can result in unexpected behavior.
...
[API 2006] | | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="657ef5ce2832047b-c42db03c-41724d2c-8149bf4f-27ca3ef336efd3b40d4f0361"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] | | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="e1c3df15aac260be-0e4287b1-47b34d10-bc7490b8-8559b68230c4498caa696a05"><ac:plain-text-body><![CDATA[ | [[Seacord 2005a | AA. Bibliography#Seacord 05]] | Chapter 7, "File I/O" | ]]></ac:plain-text-body></ac:structured-macro> |
...