Java Reference
In-Depth Information
an array of available filesystem roots and then outputs the array's contents.
Listing 8-1.
Dumping available filesystem roots to the standard output device
import java.io.File;
class DumpRoots
{
public static void main(String[] args)
{
File[] roots = File.listRoots();
for (File root: roots)
System.out.println(root);
}
}
WhenIrunthisapplicationonmyWindowsXPplatform,Ireceivethefollowingout-
put, which reveals four available roots:
A:\
C:\
D:\
E:\
If I ran
DumpRoots
on a Unix or Linux platform, I would receive one output line
consisting of the virtual filesystem root (
/
).
Apartfromusing
listRoots()
,youcanobtaina
File
instancebycallinga
File
constructorsuchas
File(String pathname)
,whichcreatesa
File
instancethat
stores the
pathname
string. The following assignment statements demonstrate this
constructor:
File file1 = new File("/x/y");
File file2 = new File("C:\\temp\\x.dat");
ThefirststatementassumesaUnixorLinuxplatform,startsthepathnamewithroot
directorysymbol
/
,andcontinueswithdirectoryname
x
,separatorcharacter
/
,andfile
ordirectoryname
y
.(ItalsoworksonWindows,whichassumesthispathbeginsatthe
root directory on the current drive.)