4.2.1 File: Sem-Iii Kns Institute of Technlogy Java Ch-4 Department of Mca
4.2.1 File: Sem-Iii Kns Institute of Technlogy Java Ch-4 Department of Mca
4.2.1 File: Sem-Iii Kns Institute of Technlogy Java Ch-4 Department of Mca
JAVA CH-4
DEPARTMENT OF MCA
4.2.1 File
Files are a primary source and destination for data within many programs.
Although there are severe restrictions on their use within applets for security reasons, files are
still a central resource for storing persistent and shared information.
A directory in Java is treated simply as a File with one additional propertya list of filenames
that can be examined by the list( ) method.
The following example creates three files: f1, f2, and f3.
The first File object is constructed with a directory path as the only argument.
The third includes the file path assigned to f1 and a filename; f3 refers to the same file as f2.
File f1 = new File("/");
File f2 = new File("/","autoexec.bat");
File f3 = new File(f1,"autoexec.bat");
E-Mail: [email protected]
18
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
OUTPUT:
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.
Here, the filename specified by newName becomes the new name of the invoking File object.
E-Mail: [email protected]
19
SEM-III
JAVA CH-4
DEPARTMENT OF MCA
It will return true upon success and false if the file cannot be renamed
2) delete()
It deletes the disk file represented by the path of the invoking File object
Syntax:
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.
Removes the file associated with the invoking object when the JVM
long getFreeSpace( ) Returns the number of free bytes of storage available on the partition
associated with the invoking object.
long getTotalSpace( ) Returns the storage capacity of the partition associated with the invoking
object. (Added by Java SE 6.)
long getUsableSpace( ) Returns the number of usable free bytes of storage available on the partition
associated with the invoking object. (Added by Java SE 6.)
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.
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.
E-Mail: [email protected]
20