THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
Returns the available filesystem roots, that is, roots of local hierarchical file
systems. Windows platforms, for example, have a root directory for each
active drive; UNIX platforms have a single / root directory. If none are
available, the array has zero elements.

The methods listFiles()andlistFiles(FilenameFilter) are analogous to list()and
list(FilenameFilter), but return arrays of File objects instead of arrays of strings. The method
listFiles(FileFilter) is analogous to the list that uses a FilenameFilter.


Three methods relate primarily to temporary files (sometimes called "scratch files")those files you need to
create during a run of your program for storing data, or to pass between passes of your computation, but
which are not needed after your program is finished.


public static FilecreateTempFile(String prefix, String suffix,
File directory)tHRows IOException

Creates a new empty file in the specified directory, using the given prefix and
suffix strings to generate its name. If this method returns successfully then it
is guaranteed that the file denoted by the returned abstract pathname did not
exist before this method was invoked, and neither this method nor any of its
variants will return the same abstract pathname again in the current
invocation of the virtual machine. The prefix argument must be at least
three characters long, otherwise an IllegalArgumentException is
thrown. It is recommended that the prefix be a short, meaningful string such
as "hjb" or "mail". The suffix argument may be null, in which case
the suffix ".tmp" will be used. Note that since there is no predefined
separator between the file name and the suffix, any separator, such as '.',
must be part of the suffix. If the directory argument is null then the
system-dependent default temporary-file directory will be used. The default
temporary-file directory is specified by the system property
java.io.tmpdir.

public static FilecreateTempFile(String prefix, String suffix)
tHRows IOException

Equivalent to createTempFile(prefix,suffix,null).

public voiddeleteOnExit()

Requests the system to remove the file when the virtual machine
terminatessee "Shutdown" on page 672. This request only applies to a normal
termination of the virtual machine and cannot be revoked once issued.

When a temporary file is created, the prefix and the suffix may first be adjusted to fit the limitations of the
underlying platform. If the prefix is too long then it will be truncated, but its first three characters will always
be preserved. If the suffix is too long then it too will be truncated, but if it begins with a period (.) then the
period and the first three characters following it will always be preserved. Once these adjustments have been
made the name of the new file will be generated by concatenating the prefix, five or more internally generated
characters, and the suffix. Temporary files are not automatically deleted on exit, although you will often
invoke deleteOnExit on File objects returned by createTempFile.


Finally, the character File.pathSeparatorChar and its companion string File.pathSeparator
represent the character that separates file or directory names in a search path. For example, UNIX separates
components in the program search path with a colon, as in ".:/bin:/usr/bin", so

Free download pdf