public URL(URL u, String s) throws MalformedURLException
URL u1, u2;
try {
URL u1 = new URL("http://www.macfaq.com/index.html");
URL u2 = new URL (u1, "vendor.html");
}
catch (MalformedURLException e) {
System.err.println(e);
}
The filename is removed from the path of u1
, and the new filename vendor.html is appended to make u2
. This constructor is particularly useful when you want to loop through a list of files that are all in the same directory. You can create a URL for the first file, and then use this initial URL to create URL objects for the other files by substituting their filenames. You also use this constructor when you want to create a URL relative to the applet's document or code base, which you retrieve using the getDocumentBase()
or getCodeBase()
methods of the Applet
class. Example 5.4 is a very simple applet that uses getDocumentBase()
to create a new URL
object. You'll need to show the Java console to see the output.
When using this constructor with getDocumentBase()
, you frequently put the call to getDocumentBase()
inside the constructor:
u2 = new URL(getDocumentBase(), "vendor.html");