Anyway, I'll brighten up the situation for anyone who doesn't know URIs: "URI" stands for Uniform Resource Identifier, and the "not to URI" part of the title refers to "URL", Uniform Resource Locator. Seriously, I don't know what the difference between them is supposed to be, just that they behave pretty differently in Java.
(And for anyone who thinks, "Well, so you do know the difference?" No, the Java classes are an implementation of a specification that I don't understand, and not even really know. I know, however, how the implementation behaves. If this behavior follows the original specification, which was not made for the Java language but presumably for the internet, I don't know.)
So, from my point of view, both URIs and URLs are meant to identify some sort of resource, like a network drive of some web site.
- In Java, you can directly connect to a URL and read from and write to it. You can't do that for a URI, but you can convert between both forms (if the resource is legal in both representations).
- The equals() and hashCode() methods of URL perform a name space lookup, making them very slow. URLs are therefore poor choices for map keys, for example for caching downloaded files.
The third difference I've experienced so far is very subtle and unexpected, and it is the real reason for this post. wow, about time to get to the point...
You can represent a file inside a Jar (or Zip) archive using a URL or URI; both work. You can also "resolve" a relative URL/URI against another one to get an absolute one. But the combination of both only works with URL!
jar:file:/path/to/archive.jar!/path/inside/archive/
jar:file:/path/to/archive.jar!/path/inside/archive.txt
are both legitimate URIs/URLs. Say, you want to resolve "hello.txt" against these, you expect:
jar:file:/path/to/archive.jar!/path/inside/archive/hello.txt
jar:file:/path/to/archive.jar!/path/inside/hello.txt however, this works only with URLs. A URI won't be able to resolve and will just return "hello.txt"
I said that I recently reimplemented TreeProperties, and I did it based on URIs. As you might guess, resolving paths relative to some context is pretty important for a tree structure. Finding the bug was very annoying, because reading from a Jar file, or even multiple Jar files, is not something I have years of experience with. Fixing the bug was way easier once I sorted out its origin.
I'd like to end this post with another Shakespeare quote:
"But none come to mind. I hope I have at least fooled you with the quotation marks. Just be aware or the differences between Uniform Resource Locators and Identifiers"