A file can be created using File class.
f = new File (string filename);
The File class is defined in the package java.io The File class is used to store the path and name of a directory or file. But this class is not useful in retrieving or storing data. The File object can be used to create, rename or delete file or directory it represents. A directory is treated as a file that contains a list of files. Each file (or directory) in Java is an object of the File class. A File object is used to manipulate the information associated with a disk file, such as last modification date and time, directory path and also to navigate through the hierarchies of sub-directories.
The File class has three constructors that are used to create a file object. They are the following:
• File(String pathname); II pathname could be file or a directory name
• File(String dirPathname, String filename); II specify directory where the file is created
II and file name
• File(File directory, String filename); II file object as the directory path
These constructors return the reference to the file object. The File class provides several methods that deal with the files, file system, properties of the file and tests on the files. Some of the important methods that serve these purposes are listed in Table.
Some methods in the File class and their description.
Name of method
string get Name ()
Returns the name of the file excluding the directory name
string get Path ()
Returns the path of the file.
string get Parent ()
Returns the parent directory of the file
Boolean exists ()
Returns true if the file exists and returns false if the file does not exist.
Boolean can Read ()
Returns true if the file is readable, otherwise returns false
Boolean can Write ()
Returns true if the file is writable, otherwise returns false
Boolean is File ()
Returns true if the object is a file, otherwise returns false.
Boolean is Directory ()
Returns true if the object is a directory, otherwise returns false.
Boolean is Absolute ()
Returns true if the file path is the absolute path, otherwise it returns false, indicating that it is a relative path.
long length ()
Returns the length of the file (in bytes).
long last Modified ()
Returns the date on which the file was last modified
String list ()
Returns the list of all files and directories that exist in the current directory
There are other methods also which are useful for handling file operations such as renaming a
the file and deleting a file. The syntax of these two methods is the following:
boolean renameTo(File new file name);
The following methods are used to create a directory:
boolean mkdir(File newdirectoryname)
boolean mkdirs(File newdirectoryname)
The first method creates a directory and returns true if it is successful. If the directory cannot be created, it returns false. In addition to creating a directory, the second method creates all its parent directories. The path of the directly to be created will be passed as a parameter to the mkdirs method. If the program fails to execute the mkdirs() method fully, it may be successful in creating partial directories list from the user specified path. The method mkdirs() returns true if the directory was created with all the necessary parent directories, otherwise it returns false. Directory is the name of an object in the File class with an extra property referred to as ‘list’ which is examined by the List () method. This method returns the string array with all files that exist in the directory. Program illustrates the List () method.
Using the methods List () and is Directory ()
public static void main(String a[ ])
String name = arg;
Filefile1= new File(name);
if ( file1.isDirectory() )
string arr = file1.list();
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i]+ “is directory”);
Program illustrates the is Directory () method, which returns the value true if the in voting file object is a directory and returns the value false if it is a file. This program is run with the command line arguments in which arg  specifies the name of the directory. The output of the program consists of a list of all the files and directories in the present directory that is, the directory given as the input.