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.

Figure 9.4. The java.util.jar package hierarchy<br />

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

All of these classes are used much like their superclasses are. For instance, to read a JAR file,<br />

follow these steps:<br />

1. Construct a JarInputStream object from an underlying stream, most commonly a file<br />

input stream.<br />

2. Open the next JAR entry in the archive.<br />

3. Read data from the JAR entry using InputStream methods like read().<br />

4. Close the JAR entry (optional)<br />

5. Repeat steps 2 through 4 as long as there are more entries (files) remaining in the<br />

archive.<br />

6. Close the JAR input stream.<br />

These are the same six steps you use to read a zip file, only with the java.util.zip classes<br />

replaced by their counterparts in java.util.jar. Even that much is unnecessary. All the<br />

standard zip tools as well as the programs developed in this chapter can work equally well<br />

with JAR files. However, the java.util.jar classes do provide some extra convenience<br />

methods for reading and writing manifest entries.<br />

9.5.4 JarFile<br />

The java.util.jar.JarFile class represents a file in the JAR format. It is a subclass of<br />

java.util.zip.ZipFile, and JarFile objects are almost exactly like ZipFile objects.<br />

public class JarFile extends ZipFile // <strong>Java</strong> 2<br />

The JarFile class has four constructors:<br />

180

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

Saved successfully!

Ooh no, something went wrong!