Java Reference
In-Depth Information
Display 10.12
Some Methods in the Class
File
(part 1 of 2)
File
is in the
java.io
package.
public
File(String
File_Name
)
Constructor.
File_Name
can be either a full or a relative path name (which includes the case of a sim-
ple file name).
File_Name
is referred to as the
abstract path name
.
public
boolean
exists()
Tests whether there is a file with the abstract path name.
public
boolean
canRead()
Tests whether the program can read from the file. Returns
true
if the file named by the abstract
path name exists and is readable by the program; otherwise returns
false
.
public boolean
setReadOnly()
Sets the file represented by the abstract path name to be read only. Returns
true
if successful; oth-
erwise returns
false
.
public
boolean
canWrite()
Tests whether the program can write to the file. Returns
true
if the file named by the abstract path
name exists and is writable by the program; otherwise returns
false
.
public
boolean
delete()
Tries to delete the file or directory named by the abstract path name. A directory must be empty to
be removed. Returns
true
if it was able to delete the file or directory. Returns
false
if it was unable
to delete the file or directory.
public boolean
createNewFile()
throws
IOException
Creates a new empty file named by the abstract path name, provided that a file of that name does
not already exist. Returns
true
if successful, and returns
false
otherwise.
public
String getName()
Returns the last name in the abstract path name (that is, the simple file name). Returns the empty
string if the abstract path name is the empty string.
public
String getPath()
Returns the abstract path name as a
String
value.
public boolean
renameTo(File
New_Name
)
Renames the file represented by the abstract path name to
New_Name
. Returns
true
if successful;
otherwise returns
false
.
New_Name
can be a relative or absolute path name. This may require mov-
ing the file. Whether or not the file can be moved is system dependent.