10 May 2004
We interrupt your regular stream of snarky political commentary with some snarky programming commentary.
I’m trying to write Java code that can locate a directory somewhere on the run-time classpath, and then iterate through the files in that directory. Here’s what I’ve discovered so far:
- The ClassLoader.getResource(String) method returns a URL object.
- One of the File constructors takes a URI argument. None of them take URLs, though.
- The documentation for the URI class assures me that “every URL is a URI, but not every URI is a URL.”
- The URI.toURL() method, as you might expect, converts a URI into a URL.
- There is, as far as I can tell, no corresponding method to convert in the other direction. I think I can fake it by calling someURI = new URI, null), except that in order for that code to compile, I have to catch URISyntaxException. But if “every URL is a URI,” I should always be able to convert from one to the other without throwing an exception, right?