Java Reference
In-Depth Information
The conversion of a pathname string to or from an abstract pathname is inherently
platform-dependent.Whenapathnamestringisconvertedtoanabstractpathname,the
names within it are separated by the default name-separator character or by any other
name-separator character that is supported by the underlying platform. For example,
File(String pathname)
converts pathname string
/x/y
to abstract pathname
/x/y
onaUnixorLinuxplatform,andthissamepathnamestringtoabstractpathname
\x\y
on a Windows platform.
Note
The
default name-separator character
is obtainable from system property
file.separator
, and is also stored in
File
's
separator
and
separator-
Char
class fields. The first field stores the character in a
java.lang.String
in-
stance and the second field stores it as a
char
value. Neither name of these
final
fields follows the convention of appearing entirely in uppercase.
Whenanabstractpathnameisconvertedintoapathnamestring,eachnameissepar-
ated from the next by a single copy of the default name-separator character.
File
offersadditionalconstructorsforinstantiatingthisclass.Forexample,thefol-
lowing constructors merge parent and child pathnames into combined pathnames that
are stored in
File
objects:
•
File(String parent, String child)
createsanew
File
instance
from a
parent
pathname string and a
child
pathname string.
•
File(File parent, String child)
creates a new
File
instance
from a
parent
pathname
File
instance and a
child
pathname string.
Eachconstructor's
parent
parameterispasseda
parent pathname
,astringthatcon-
sistsofallpathnamecomponentsexceptforthelastname,whichisspecifiedby
child
.
The following statement demonstrates this concept via
File(String, String)
:
File file3 = new File("prj/books/", "bj7");
Theconstructormergesrelativeparentpathname
prj/books/
withchildpathname
bj7
into relative pathname
prj/books/bj7
. (If I had specified
prj/books
as
the parent pathname, the constructor would have added the separator character after
books
.)
Tip
Because
File(String pathname)
,
File(String parent,
String child)
, and
File(File parent, String child)
don't detect
invalid pathname arguments (apart from throwing
java.lang.NullPointerException
when
pathname
or
child
is
null
),
you must be careful when specifying pathnames. You should strive to only specify