img
When you run this program, you will see something similar to the following:
File Name: COPYRIGHT
Path: /java/COPYRIGHT
Abs Path: /java/COPYRIGHT
Parent: /java
exists
is writeable
is readable
is not a directory
is normal file
is absolute
File last modified: 812465204000
File size: 695 Bytes
Most of the File methods are self-explanatory. isFile( ) and isAbsolute( ) are not. isFile( )
returns true if called on a file and false if called on a directory. Also, isFile( ) returns false
for some special files, such as device drivers and named pipes, so this method can be used
to make sure the file will behave as a file. The isAbsolute( ) method returns true if the file
has an absolute path and false if its path is relative.
File also includes two useful utility methods. The first is renameTo( ), shown here:
boolean renameTo(File newName)
Here, the filename specified by newName becomes the new name of the invoking File object.
It will return true upon success and false if the file cannot be renamed (if you either attempt
to rename a file so that it moves from one directory to another or use an existing filename,
for example).
The second utility method is delete( ), which deletes the disk file represented by the path
of the invoking File object. It is shown here:
boolean delete( )
You can also use delete( ) to delete a directory if the directory is empty. delete( ) returns true
if it deletes the file and false if the file cannot be removed.
Here are some other File methods that you will find helpful.
Method
Description
void deleteOnExit( )
Removes the file associated with the invoking object when
the Java Vir tual Machine terminates.
long getFreeSpace( )
Returns the number of free bytes of storage available on
the par tition associated with the invoking object. (Added
by Java SE 6.)
long getTotalSpace( )
Returns the storage capacity of the par tition associated
with the invoking object. (Added by Java SE 6.)
long getUsableSpace( )
Returns the number of usable free bytes of storage
available on the par tition associated with the invoking
object. (Added by Java SE 6.)
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home