THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
deletion succeeded. Directories must be empty before they are deleted.

There are methods to create an underlying file or directory named by the current File:


public booleancreateNewFile()

Creates a new empty file, named by this File. Returns false if the file
already exists or if the file cannot be created. The check for the existence of
the file and its subsequent creation is performed atomically with respect to
other file system operations.

public booleanmkdir()

Creates a directory named by this File, returning TRue on success.

public booleanmkdirs()

Creates all directories in the path named by this File, returning TRue if all
were created. This is a way to ensure that a particular directory is created,
even if it means creating other directories that don't currently exist above it in
the directory hierarchy. Note that some of the directories may have been
created even if false is returned.

However, files are usually created by FileOutputStream or FileWriter objects or
RandomAccessFile objects, not using File objects.


Two methods let you change the state of the underlying file, assuming that one exists:


public booleansetLastModified(long time)

Sets the "last modified" time for the file or returns false if it cannot do so.

public booleansetReadOnly()

Makes the underlying file unmodifiable in the file system or returns false
if it cannot do so. The file remains unmodifiable until it is deleted or
externally marked as modifiable againthere is no method for making it
modifiable again.

There are methods for listing the contents of directories and finding out about root directories:


public String[]list()

Lists the files in this directory. If used on something that isn't a directory, it
returns null. Otherwise, it returns an array of file names. This list includes
all files in the directory except the equivalent of "." and ".." (the current
and parent directory, respectively).

public String[]list(FilenameFilter filter)

Uses filter to selectively list files in this directory (see
FilenameFilter described in the next section).

public static File[]listRoots()
Free download pdf