Resource names must be made up of one or more valid identifiers separated by / characters, specifying a path
to the resource. Typically, a class loader will search for resources in the same places it will search for class
files.
Two other resource methodsgeTResource and getSystemResourcereturn URL objects that name the
resources. The class java.net.URL is covered briefly on page 725; it provides methods to use uniform
resource locators to access resources. You can invoke the getContents method on the URL objects
returned by the class loader methods to get an object that represents the contents of that URL.
The getresources method returns a java.util.Enumeration object (an older variant of the
Iterator you've seen before) that can step through URL objects for all the resources stored under a given
name. The static method getSystemResources does the same for system resources.
The resource-getting methods first ask the parent class loader for the resource, or they ask the bootstrap class
loader if there is no parent. If the resource cannot be found, then findResource or findResources is
invoked. Just as loadClass is built on top of a findClass that you provide when you subclass
ClassLoader, so the resource methods are built on top of two methods that you can override to find
resources:
public URLfindResource(String name)
Returns a URL object for the resource of the given name, or null if none
was found. If there are multiple resources of the same name the
implementation determines which should be returned. The default
implementation in ClassLoader always returns null.
public Enumeration<URL>findResources(String name)
Returns an Enumeration of URL objects for all the resources of the given
name. The default implementation in ClassLoader returns an enumeration
through zero resources.
Here is a findResource for PlayerLoader:
public java.net.URL findResource(String name) {
File f = fileFor(name);
if (!f.exists())
return null;
try {
return f.toURL();
} catch (java.net.MalformedURLException e) {
return null; // shouldn't happen
}
}
The fileFor method is analogous to the streamFor method of PlayerLoader. It returns a
java.io.File object that corresponds to the named resource in the file system. If the named file actually
exists, then the URL for the resource is created from the path. (The File class represents a pathname in the
file systemsee "The File Class" on page 543.)
The BoldPlayer class might come with a BoldPlayer.book that can be found in the same place as the
class file. You can replace this version with your own by simply placing yours in a location where it will be