Java Reference
In-Depth Information
Method
Description
setReadOnly()
Sets the file represented by the current object as read-only and
returns
true
if the operation is successful.
mkdir()
Creates a directory with the path specified by the current
File
object. The method will fail if the parent directory of the directory
to be created does not already exist. The method returns
true
if it
is successful and
false
otherwise.
mkdirs()
Creates the directory represented by the current
File
object,
including any parent directories that are required. It returns
true
if the new directory is created successfully, and
false
otherwise.
Note that even if the method fails, some of the directories in the
path may have been created.
createNewFile()
Creates a new empty file with the pathname defined by the current
File
object as long as the file does not already exist. The method
returns
true
if the file was created successfully. Note that this
method will only create a file in an existing directory - it will not
create any directories specified by the path.
createTempFile
(String prefix,
String suffix,
File directory)
This is a static method that creates a temporary file in the
directory
directory
, with a name created using the first two
arguments, and returns a
File
object corresponding to the file
created. The string
prefix
represents the start of the file name
and must be at least three characters long. The string
suffix
specifies the file extension. The file name will be formed from
prefix
followed by five or more generated characters, followed
by
suffix
. If
suffix
is
null
,
.tmp
will be used. If
prefix
or
suffix
are too long for file names on the current system, they
will be truncated, but neither will be truncated to less than three
characters. If the
directory
argument is
null
, the system
temporary directory will be used. If the file cannot be created, an
IOException
will be thrown. If
prefix
has less than three
characters, an
IllegalArgumentException
will be thrown.
createTempFile
(String prefix,
String suffix)
Equivalent to
createTempFile(String prefix,
String suffix,
null).
delete()
This will delete the file or directory represented by the current
File
object and return
true
if the delete was successful. It won't
delete directories that are not empty. To delete a directory, you
must first delete the files it contains.
deleteOnExit()
Causes the file or directory represented by the current
File
object to be deleted when the program ends. This method does not
return a value. The deletion will only be attempted if the JVM
terminates normally. Once you call the method for a
File
object,
the delete operation is irrevocable, so you will need to be cautious
with this method.