23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

Notice the strange URL in the previous code. A JAR URL is like a normal HTTP or file URL<br />

pointing to a JAR file with the prefix "jar:" added to the URL's scheme (i.e., jar:http: or<br />

jar:file:). After the hostname, you place the pathname to the JAR file on the server. After the<br />

JAR filename, you add an exclamation point and a path to the particular entry you want<br />

within the JAR archive. For example, to refer to the file StreamCopier.class in the<br />

com/macfaq/io directory of the JAR file javaio.jar located at http://www.oreilly.com/, you<br />

would use the JAR URL<br />

jar:http://www.oreilly.com/javaio.jar!/com/macfaq/io/StreamCopier.class. If the entry is<br />

omitted, then the URL refers to the JAR archive as a whole; for example,<br />

jar:http://www.oreilly.com/javaio.jar!/.<br />

If you only want to read data from the connection using getInputStream() from the<br />

URLConnection superclass, the previous code will suffice. If you want to use the methods of<br />

the JarURLConnection class directly, then you should cast the object returned from<br />

openConnection() to JarURLConnection. For example:<br />

try {<br />

URL u = new URL(<br />

"jar:http://www.oreilly.com/javaio.jar!/com/macfaq/io/StreamCopier.class");<br />

JarURLConnection juc = (JarURLConnection) u.openConnection();<br />

// ...<br />

}<br />

catch (MalformedURLException e) {<br />

// ...<br />

}<br />

Once you've done this, you can use eight methods that provide easy access to various metainformation<br />

about the JAR file and its contents. This meta-information comes from the<br />

archive's manifest or certificate files. The getJarFileURL() method returns the URL of the<br />

JAR file for this connection:<br />

public URL getJarFileURL()<br />

This is most useful if the URL refers to a particular entry in the file. In that instance, the URL<br />

returned by getJarFileURL() refers to the URL of the archive. For example:<br />

URL u = new URL(<br />

"jar:http://www.oreilly.com/javaio.jar!/com/macfaq/io/StreamCopier.class");<br />

JarURLConnection juc = (JarURLConnection) u.openConnection();<br />

URL base = juc.getURL();<br />

The URL object base now refers to http://www.oreilly.com/javaio.jar.<br />

The getEntryName() method returns the name of the JAR entry this JAR URL refers to. It<br />

returns null if the JAR URL points to a JAR file as a whole rather than to one of the entries<br />

in the file.<br />

public String getEntryName()<br />

The getJarFile() method returns an immutable JarFile object for the JAR archive referred<br />

to by this URL. You can read the state of this object, but you cannot change it. Attempts to do<br />

188

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!