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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

assume the array returned by listRoots() necessarily has any members! null is returned if<br />

the list can't be determined at all. This is not the same thing as a zero-length array.<br />

The list of roots may or may not contain drives that are mounted over the network. If the drive<br />

is mounted in such a fashion that it pretends to be a local drive, it probably will be in the list.<br />

If the filesystem does not look like a local drive, it probably won't appear in the list. For<br />

instance, on Windows, network drives mapped to letters appear, but drives with UNC<br />

pathnames do not. Example 12.1 is a very simple program to list the roots and print them.<br />

Example 12.1. RootLister<br />

import java.io.*;<br />

public class RootLister {<br />

}<br />

public static void main(String[] args) {<br />

}<br />

File[] roots = File.listRoots();<br />

for (int i = 0; i < roots.length; i++) {<br />

System.out.println(roots[i]);<br />

}<br />

Here's the output produced by RootLister on my Windows NT system. A: is the floppy<br />

drive. This system doesn't have a second floppy, which would normally be B:. C:, D:, E:, and<br />

F: are all partitions of the primary hard drive that appear to be separate drives. G: is the Zip<br />

drive, and H: is the CD-ROM. I: is a Macintosh drive mounted over the LAN.<br />

D:\JAVA\ioexamples\12>java RootLister<br />

A:\<br />

C:\<br />

D:\<br />

E:\<br />

F:\<br />

G:\<br />

H:\<br />

I:\<br />

The output on Unix is much simpler and is virtually guaranteed to look like this:<br />

% java RootLister<br />

/<br />

Since <strong>Java</strong> 2 isn't available for the Mac at the time of this writing, I can only speculate about<br />

what the output is likely to be on that platform. However, it will probably look something like<br />

this:<br />

Macintosh HD<br />

System 8<br />

Drive D<br />

Drive F<br />

The exact names would depend on the names the user gave to the individual hard drives and<br />

network servers.<br />

283

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

Saved successfully!

Ooh no, something went wrong!