Java Reference
In-Depth Information
getName() returns "mybooks" for myDirectory1 and myDirectory2.
getPath() returns "\documents\mybooks" for myDirectory1 and "mybooks"
for myDirectory2
getParent()
returns "\documents" for myDirectory1and null for
myDirectory2
you may create a
File
instance even when the file or directory it represents does not
exist. None of the constructors throw an
IOException
or
FileNotFoundException
if
the file or directory does not exist (they will throw a
NullPointerException
if the
pathName
or child is null). This capability is needed to invoke the various methods
to create directories and files. If you need to know whether the directory or file ex-
ists,
File
provides the
exists()
method that returns true if the entity exists and false
if it does not. The
isDirectory()
method returns true if the
File
entity is a direc-
tory and exists; otherwise, it returns false. The
isFile()
method returns true if the
entity is a file (not a directory) and exists; otherwise, it returns false.
For both files and directories, the
File
class provides access to the characteris-
tics of the file:
Method
Description
length()
Returns the size of the file or directory
lastModified()
Returns the time stamp of the file or directory
isHidden()
Tests to see if file or directory is hidden
canRead()
Tests to see if you have read access to the file or directory
canWrite()
Tests to see if you have write access to the file or directory