Java Reference
In-Depth Information
Discussion
Speaking of directory listings, you surely know that all modern desktop computing systems
arrange files into hierarchies of directories. But you might not know that on Unix all file-
names are somehow “under” the single root directory named / , whereas on Microsoft plat-
forms, each disk drive has a root directory named \ (A:\ for the first floppy (if you still have
one!), C:\ for the first hard drive, and other letters for CD-ROM and network drives). If you
need to know about all the files on all the disks, you should find out what “directory root”
names exist on the particular platform. The static method listRoots() returns (in an array
of File objects) the available filesystem roots for whatever platform you are running on.
Here is a short program to list these, along with its output:
dir_file/ListRoots.java
public
public class
class ListRoots
ListRoots {
public
public static
void main ( String argh_my_aching_fingers []) {
File [] drives = File . listRoots (); // Get list of names
for
static void
for ( File dr : drives ) {
System . out . println ( dr );
// Print the list
}
}
}
C:> java dir_file.DirRoots
A:\
C:\
D:\
C:>
As you can see, the program listed my floppy drive (even though the floppy drive was not
only empty, but left at home while I wrote this recipe on my notebook computer in my car in
a parking lot), the hard disk drive, and the CD-ROM drive.
On Unix there is only one:
$ java dir_file.DirRoots
/
$
Search WWH ::




Custom Search