How can I skip over files that I don't have enough permission to view?
You can't.
You have to actually try to read the file to find out if you can actually read the file. See How to check if a file is readable?
In general, "Use X to see if I can do Y" is a fundamentally incorrect approach. X is not Y, so doing X successfully doesn't mean Y will work, nor does X failing indicate that Y will fail. As the linked question shows, no version of Java's isReadable()
will give you definitive results as to whether or not you can actually read the file.
Second, even if you do somehow come up with a check that is in fact perfect (you won't, because for example you can't even see any SELinux rules that may be in place...), you still have a TOCTOU bug - things change.
The isReadable()
documentation even states:
Note that the result of this method is immediately outdated, there is no guarantee that a subsequent attempt to open the file for reading will succeed
Just because your check said you can read the file doesn't mean you can actually read the file when you try.
The only definitive way to determine that you can actually read a file is to actually read that file. Any checks you do prior to actually trying to open the file for reading just add useless overhead.
You still have to properly handle exceptions at any location of your code.