Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 terminology is confusing and sometimes befuddling, and mostly born from the evolution both of Java as an API and as a platform over time. To understand how these terms came to mean what they do, it is important to recognise two things that influence Java's design:

  • Backwards compatibility. Old applications should run on newer installations, ideally without modification. This means that an old API (with its names and terminology) needs to be maintained through all newer versions.
  • Cross-platform. The API should provide a usable abstraction of its underlying platform, whether that be an operating system or a browser.

I'll walk through the concepts and how they came to be. I'll answer your other, specific questions after that, because I may have to refer to something in the first part.

What is a "Resource"?

An abstract, generic piece of data that can be located and read. Loosely said, Java uses this to refer to a "file" that may not be a file but does represent a named piece of data. It does not have a direct class or interface representation in Java, but because of its properties (locatable, readable) it is often represented by an URL.

Because one of Java's early design goals was to be run inside of a browser, as a sandboxed application (applets!) with very limited rights/privileges/security clearance, Java makes a clear (theoretical) difference between a file (something on the local file system) and a resource (something it needs to read). This is why reading something relative to the application (icons, class files, and so on) is done through ClassLoader.getResource and not through the File class.

Unfortunately, because "resource" is also a useful generic term outside of this interpretation, it is also used to name very specific things (e.g. class ResourceBundle, UIResource, Resource) that are not, in this sense, a resource.

The main classes representing (a path to) a resource are java.nio.file.Path, java.io.File, java.net.URI, and java.net.URL.

File (java.io, 1.0)

An abstract representation of file and directory pathnames.

The File class represents a resource that is reachable through the platform's native file system. It contains only the name of the file, so it is really more a path (see later) that the host platform interprets according to its own settings, rules, and syntax.

Note that File doesn't need to point to something local, just something that the host platform understands in the context of file access, e.g. a UNC path in Windows. If you mount a ZIP file as a file system in your OS, then File will read its contained entries just fine.

URL (java.net, 1.0)

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.

In tandem with the concept of a resource, the URL represents that resource the same way the File class represents a file in the host platform: as a structured string that points to a resource. URL additionally contains a scheme that hints at how to reach the resource (with "file:" being "ask the host platform"), and so allows pointing at resources through HTTP, FTP, inside a JAR, and whatnot.

Unfortunately, URLs come with their own syntax and terminology, including the use of "file" and "path". In case the URL is a file-URL, URL.getFile will return a string identical to the path string of the referenced file.

Class.getResource returns an URL: it is more flexible than returning File, and it has served the needs of the system as imagined in the early 1990's.

URI (java.net, 1.4)

Represents a Uniform Resource Identifier (URI) reference.

URI is a (slight) abstraction over URL. The difference between URI and URL is conceptual and mostly academic, but URI is better defined in a formal sense, and covers a wider range of use cases. Because URL and URI are/were not the same thing, a new class was introduced to represent them, with methods URI.toURL and URL.toURI to move between one and the other.

In Java, the main difference between URL and URI is that an URL carries the expectation of being resolvable, something the application might want an InputStream from; an URI is treated more like an abstract thingamajig that might point to something resolvable (and usually does), but what it means and how to reach it are more open to context and interpretation.

Path (java.nio.file, 1.7)

An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.

The new file API, iconified in the Path interface, allows for much greater flexibility than the File class could offer. The Path interface is an abstraction of the File class, and is part of the New IO File API. Where File necessarily points to a "file" as understood by the host platform, Path is more generic: it represents a file (resource) in an arbitrary file system.

Path takes away the reliance on the host platform's concept of a file. It could be an entry in a ZIP file, a file reachable through FTP or SSH-FS, a multi-rooted representation of the application classpath, or really anything that can be meaningfully represented through the FileSystem interface and its driver, FileSystemProvider. It brings the power of "mounting" file systems into the context of a Java application.

The host platform is represented through the "default file system"; when you call File.toPath, you get a Path on the default file system.


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?

Unlikely. If the jar file is on the local file system, you should not have a query component, so URL.getPath and URL.getFile should return the same result. However, pick the one you need: file-URLs may not typically have query components, but I could sure add one anyway.

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

URL might not be enough because File gives you access to housekeeping data such as permissions (readable, writable, executable), file type (am I a directory?), and the ability to search and manipulate the local file system. If these are features you need, then File or Path provide them.

You don't need File if you have access to Path. Some older API may require File, though.

(And is there a Resource object?)

No, there isn't. There are many things named like it, but they are not a resource in the sense of ClassLoader.getResource.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...