jbanana: Badly drawn banana (Default)
J Banana ([personal profile] jbanana) wrote2021-04-07 11:33 am
Entry tags:

File lightbulb moment

I was always puzzled by something in the File class. It has getAbsolutePath() and getCanonicalPath() and they both do almost the same thing: return a fully qualified name of the file. One difference is that the "absolute" version doesn't simplify paths with ".." which makes the "canonical" version always looks tempting. But that version throws IOException, so you need a try block, which makes it a pain.

How could cleaning up a file path cause an IOException? Documentation says "construction of the canonical pathname may require filesystem queries" and I've never known what that could mean.

Today I passed a ridiculously long name to the "canonical" version and it said IOException: The parameter is incorrect. Shortening the name made it work. I think it's telling me "that file cannot possibly exist on this file system", which kind-of fits into the "filesystem queries" category mentioned in the docs.