Java Reference
In-Depth Information
Note
A
path
isahierarchyofdirectoriesthatmustbetraversedtolocateafileora
directory.A
pathname
isastringrepresentationofapath;aplatform-dependent
separ-
ator character
(suchastheWindowsbackslash[
\
]character)appearsbetweenconsec-
utive names.
The second statement assumes a Windows platform, starts the pathname with drive
specifier
C:
,andcontinueswithrootdirectorysymbol
\
,directoryname
temp
,separ-
ator character
\
, and filename
x.dat
(although
x.dat
might refer to a directory).
Caution
Always double backslash characters that appear in a string literal, espe-
ciallywhenspecifyingapathname;otherwise,youruntheriskofbugsorcompilerer-
rormessages.Forexample,Idoubledthebackslashcharactersinthesecondstatement
todenoteabackslashandnotatab(
\t
),andtoavoidacompilererrormessage(
\x
is
illegal).
Eachstatement'spathnameisan
absolute pathname
,whichisapathnamethatstarts
withtherootdirectorysymbol;nootherinformationisrequiredtolocatethefile/direct-
orythatitdenotes.Incontrast,a
relative pathname
doesn'tstartwiththerootdirectory
symbol; it's interpreted via information taken from some other pathname.
Note
The
java.io
package's classes default to resolving relative pathnames
against the current user (also known as working) directory, which is identified by
system property
user.dir
, and which is typically the directory in which the JVM
was launched. (
Chapter 4
shows you how to read system properties via the
java.lang.System
class's
getProperty()
method.)
File
instances contain abstract representations of file and directory pathnames
(thesefilesordirectoriesmayormaynotexistintheirfilesystems)bystoring
abstract
pathnames
,whichofferplatform-independentviewsofhierarchicalpathnames.Incon-
trast,userinterfacesandoperatingsystemsuseplatform-dependent
pathname strings
to
name files and directories.
Anabstractpathnameconsistsofanoptionalplatform-dependent prefixstring,such
asadisk-drivespecifier,“/”fortheUnixrootdirectory,or“\\”foraWindowsUniversal
Naming Convention (UNC) pathname; and a sequence of zero or more string names.
Thefirstnameinanabstractpathnamemaybeadirectorynameor,inthecaseofWin-
dowsUNCpathnames,ahostname.Eachsubsequentnamedenotesadirectory;thelast
namemaydenoteadirectoryorafile.The
empty abstract pathname
hasnoprefixand
an empty name sequence.