What's the Difference Between a Resource, Uri, Url, Path and File in Java

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

UPDATE 2017-04-12 Check JvR's answer as it contains more exhaustive and exact explanation!


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).

What is the difference between URI, URL and URN?

A Uniform Resource Identifier (URI) is a string of characters used to identify a name or a resource on the Internet.

A URI identifies a resource either by location, or a name, or both. A URI has two specializations known as URL and URN.

A Uniform Resource Locator (URL) is a subset of the Uniform Resource Identifier (URI) that specifies where an identified resource is available and the mechanism for retrieving it. A URL defines how the resource can be obtained. It does not have to be a HTTP URL (http://), a URL can also start with ftp:// or smb://, specifying the protocol that's used to get the resource.

A Uniform Resource Name (URN) is a Uniform Resource Identifier (URI) that uses the URN scheme, and does not imply availability of the identified resource. Both URNs (names) and URLs (locators) are URIs, and a particular URI may be both a name and a locator at the same time.

This diagram (source) visualizes the relationship between URI, URN, and URL:

URI, URN, URL diagram

The URNs are part of a larger Internet information architecture which is composed of URNs, URCs and URLs.

bar.html is not a URN. A URN is similar to a person's name, while a URL is like a street address. The URN defines something's identity, while the URL provides a location. Essentially URN vs. URL is "what" vs. "where". A URN has to be of this form <URN> ::= "urn:" <NID> ":" <NSS> where <NID> is the Namespace Identifier, and <NSS> is the Namespace Specific String.

To put it differently:

  • A URL is a URI that identifies a resource and also provides the means of locating the resource by describing the way to access it
  • A URL is a URI
  • A URI is not necessarily a URL

I'd say the only thing left to make it 100% clear would be to have an example of an URI that is not an URL. We can use the examples in RFC3986:

URL: ftp://ftp.is.co.za/rfc/rfc1808.txt
URL: http://www.ietf.org/rfc/rfc2396.txt
URL: ldap://[2001:db8::7]/c=GB?objectClass?one
URL: mailto:John.Doe@example.com
URL: news:comp.infosystems.www.servers.unix
URL: telnet://192.0.2.16:80/
URN (not URL): urn:oasis:names:specification:docbook:dtd:xml:4.1.2
URN (not URL): tel:+1-816-555-1212 (disputed, see comments)

Files, URIs, and URLs conflicting in Java

The problem is that you use URL to construct the second file:

File myFile = new File(userURL.getFile());

If you stick to the URI, you are better off:

URI userURI = userFile.toURI();
URL userURL = userURI.toURL();
...
File myFile = new File(userURI);

or

File myFile = new File( userURL.toURI() );

Both ways worked for me, when testing file names with blanks.

What's the difference between path and URL in iOS?

URL includes the protocol being used (http:// etc). Path doesn't or doesn't need at least.



Related Topics



Leave a reply



Submit