Christian Christian - 15 days ago 10
Java Question

What's the difference between a Resource, URI, URL, Path and File in Java?

I'm looking at a piece of Java code right now, and it takes a path as a String and gets its URL using

URL resource = ClassLoader.getSystemClassLoader().getResource(pathAsString);
, then calls
String path = resource.getPath()
and finally executes
new File(path);
.

Oh, and there are also calls to
URL url = resource.toURI();
and
String file = resource.getFile()
.

I'm totally confused right now - mostly because of the terminology, I guess. Can someone please walk me through the differences, or provide a few links to Dummy-proof material? Especially URI to URL and Resource to File? To me, it feels like they should be the same thing, respectively...

The difference between
getFile()
and
getPath()
is explained here: What's the difference between url.getFile() and getpath()? (Interestingly they both seem to return Strings, which probably adds a whole lot to my state of mind...)

Now, if I have a locator that references a class or package in a jar file, will those two (i.e. path an file strings) differ?

resource.toString()
would give you
jar:file:/C:/path/to/my.jar!/com/example/
, after all (note the exclamation mark).

Is the difference between URI and URL in Java that the former doesn't encode spaces? Cf. Files, URIs, and URLs conflicting in Java (This answer explains the general, conceptual difference between the two terms fairly well: URIs identify and URLs locate;)

Lastly - and most importantly - why do I need
File
object; why isn't a Resource (
URL
) enough?
(And is there a Resource object?)

Sorry if this question is a bit unorganized; it just reflects the confusion I have... :)

Answer

Please note that I do not consider myself 100% competent to answer, but nevertheless here are some comments:

  • File represents a file or directory accessible via file system
  • resource is a generic term for a data object which can be loaded by the application
    • usually resources are files distributed with the application / library and loaded via class-loading mechanism (when they reside on class-path)
  • URL#getPath is getter on the path part of URL (protocol://host/path?query)
  • URL#getFile as per JavaDoc returns path+query

In Java, URI is just a data structure for manipulating the generic identifier itself.

URL on the other hand is really a resource locator and offers you features to actually read the resource via registered URLStreamHandlers.

URLs can lead to file-system resources and you can construct URL for every file system resource by using file:// protocol (hence File <-> URL relation).

Also be aware that that URL#getFile is unrelated to java.io.File.


Why do I need File object; why isn't a Resource (URL) enough?

It is enough. Only if you want to pass the resource to some component which can work only with files, you need to get File from it. However not all resource URLs can be converted to Files.

And is there a Resource object?

From the JRE point of view, it's just a term. Some frameworks provide you with such class (e.g. Spring's Resource).