4.2.1 File: Sem-Iii Kns Institute of Technlogy Java Ch-4 Department of Mca

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SEM-III

KNS INSTITUTE OF TECHNLOGY

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 constructors can be used to create File objects:


File(String directoryPath)
File(String directoryPath, String filename)
File(File dirObj, String filename)
File(URI uriObj)
directoryPath It is the path name of the file, filename is the name of the file or
subdirectory
dirObj It is a File object that specifies a directory
uriObj It is a URI object that describes a file.

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 second includes two argumentsthe path and the filename.

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");

Lecturer: Syed Khutubuddin Ahmed

E-Mail: [email protected]

18

SEM-III

KNS INSTITUTE OF TECHNLOGY

JAVA CH-4

DEPARTMENT OF MCA

The Program below demonstrates details about a File:


// Demonstrate File.
import java.io.File;
class FileDemo {
static void p(String s) {
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File("/java/COPYRIGHT");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Abs Path: " + f1.getAbsolutePath());

p("Parent: " + f1.getParent());


p(f1.exists() ? "exists" : "does not exist");
p(f1.canWrite() ? "is writeable" : "is not writeable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
p(f1.isFile() ? "is normal file" : "might be a named
pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified: " + f1.lastModified());
p("File size: " + f1.length() + " Bytes");
}}

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.

File also includes two useful utility methods:


1) renameTo( )
2) delete()
1) renameTo() used to rename the file
Syntax:

boolean renameTo(File newName)

Here, the filename specified by newName becomes the new name of the invoking File object.

Lecturer: Syed Khutubuddin Ahmed

E-Mail: [email protected]

19

SEM-III

KNS INSTITUTE OF TECHNLOGY

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.

File Method Description:


void deleteOnExit( )
terminates.

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.

Lecturer: Syed Khutubuddin Ahmed

E-Mail: [email protected]

20

You might also like