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.

12.2.2.1 Absolute paths<br />

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

On Unix all mounted disks, whether local or mounted over the network, are combined into a<br />

single virtual filesystem. The root of this filesystem is the directory called /. You generally do<br />

not need to concern yourself with which physical disk any particular directory resides on, as<br />

long as that disk has sufficient space. Absolute paths always begin with the root directory, /.<br />

On Windows and the Mac, there is no root directory. Each mounted disk partition or network<br />

server is a separate and independent filesystem. On Windows, these disks are assigned drive<br />

letters. A: is normally the floppy drive. B: is the second floppy drive (less common these<br />

days, since fewer systems have multiple floppy drives.) C: is the primary boot disk. D: is<br />

often reserved for a CD-ROM, though it can be an additional hard disk or partition as well. E:<br />

through Z: can be used for further disks, partitions, or network servers. A full pathname<br />

begins with the drive letter where the file resides, e.g.,<br />

C:\PUBLIC\HTML\JAVAFAQ\INDEX.HTM.<br />

On Windows 95, 98 and NT, an additional level is possible by specifying remote machines on<br />

the network like this: \\B<strong>IO</strong>\C\PUBLIC\HTML\ JAVAFAQ\INDEX.HTM. This path refers to a<br />

file called INDEX.HTM in the directory JAVAFAQ in the directory HTML in the directory<br />

PUBLIC on the C drive of the machine B<strong>IO</strong>. <strong>Java</strong> 1.1 does not provide a pure <strong>Java</strong> means to<br />

get a list of all the available volumes on Windows; the best you can do is check all possible<br />

drive letters and catch any <strong>IO</strong>Exceptions that occur. <strong>Java</strong> 2 does add a File.listRoots()<br />

method that returns all the roots of the local system.<br />

Like Windows, the Macintosh does not have a true root from which all mounted disks and<br />

servers can be accessed. Each disk or disk partition and each server appear as a separate<br />

volume, though the names are generally a little more descriptive than A, B, C, and D.<br />

Macintosh virtual machines interpret a request to list the contents of the directory "/" as a<br />

request for a list of all mounted volumes. You should be warned, however, that a single<br />

Macintosh can have several volumes with the same name. This is rare on a single-user Mac<br />

but relatively common in networked environments where file sharing can mount volumes on<br />

other Macs with common names like Macintosh HD or untitled or Public Folder. This isn't a<br />

problem for native code, which doesn't use names to identify volumes, but it's a real<br />

showstopper for pure <strong>Java</strong> programs. To further complicate matters, the Mac's primary hard<br />

drive may not be named Macintosh HD.<br />

For these reasons and more, absolute pathnames are a royal pain to work with across<br />

platforms. You should avoid hardcoding them in your programs whenever possible. Instead,<br />

you should calculate them at runtime from system properties and user input.<br />

12.2.2.2 Relative paths<br />

The following are some examples of relative paths:<br />

Unix html/index.html<br />

DOS HTML\INDEX.HTM<br />

Win32 html\index.html<br />

MacOS :html:index.html<br />

Unix index.html<br />

DOS INDEX.HTM<br />

278

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

Saved successfully!

Ooh no, something went wrong!