dev@glassfish.java.net

FindBugs errors - ignoring return value from filesystem operations

From: Bill Shannon <bill.shannon_at_oracle.com>
Date: Mon, 18 Apr 2011 15:07:30 -0700

FindBugs complains if you ignore the error return value from a filesystem
operation, e.g.,

        f.mkdirs();

Be careful when fixing these errors. In many cases it's ok if the operation
fails, e.g., if the directory already exists. The fix for the above is NOT

        if (!f.mkdirs())
            error(); // WRONG

A better fix is

        if (!f.isDirectory() && !f.mkdirs())
            error();



Also, be careful with filesystem operations that might behave differently
on Windows. I recently got bit by some permission changing code. When I
fixed it to fail if the permissions weren't changed, it failed on Windows.
The permission changing method as currently implemented always fails on
Windows. Ouch. In this case I chose to always ignore the return value.