< Prev
Next >

Java File Class

File class is used to create, delete, rename, access file or directory in our system. It is also used to check or set some important properties of a file or directory present on a physical disk of a computer, for example - We can even set access restrictions like making a file or a directory read only/writable.


A directory is also referenced by a File object in Java.

Constructors :

A few most used constructors of File class are explained with an example below :
File(String path)
File file= new File("D:\\Textbook.txt");
This example creates a File object, which is referencing a file TextBook.txt in D: drive, by a String path -"D://TextBook.txt".
File(File parent, String child)
File dir = new File("D:\\");
File file= new File(dir, "Textbook.txt");
This example creates a new file TextBook.txt at a path assigned to parent File object, dir - "D://". Hence, file TextBook.txt will be created in D: Drive.

Methods of File class

Methods Description
boolean createNewFile() This method creates a file on the disk. It returns true if file is successfully created and false if it is not.
boolean exists() This method returns true if a file/directory exists and it returns false, if it does not exist.
String getName() This method returns the name of the file or a directory.
String getParent() This method returns the name of the Parent directory in which a file/directory is located, if there is no parent directory then null is returned.
boolean isDirectory() This method returns true if a File object points to a directory otherwise it returns false.
boolean isFile() This method returns true if a File object points to a file on disk otherwise it returns false.
boolean isHidden() This method returns true if a File object points to a file which is hidden.
boolean canRead() This method returns true if a File object points to a file which can be read and has no read lock on it.
boolean canWrite() This method returns true if a File object points to a file which can be write to and has no write lock on it.
long lastModified() This method returns the time in milliseconds when a file was modified.
long length() This method returns the length of a file(in terms of bytes) referenced by File object.


Program to understand functionalities of various methods of File class.


class A
public static void main(String... ar)
File file= new File("D:\\TextBook1.txt");
{	//calling createNewFile() method creates a new File
	System.out.println("Is the file created ? "+ file.createNewFile()); 
catch(IOException e)

System.out.println("Does this file exists? "+file.exists());
System.out.println("The name of file is  "+file.getName());
System.out.println("Parent Directory of this file  "+file.getParent());
System.out.println("Path to this file "+file.getPath());
System.out.println("The full path to this file "+file.getAbsolutePath());
System.out.println("Is this a Directory? "+file.isDirectory());
System.out.println("Is this a File? "+file.isFile());
System.out.println("Is this a hidden file "+file.isHidden());
System.out.println("Is this File readable ? "+file.canRead());
System.out.println("Is this File writable ? "+file.canWrite());

}	//main method ends
}	//class definition ends

Output is -:

Does this file exists? true
The name of file is  TextBook1.txt
Parent Directory of this file  D:\
Path to this file D:\TextBook1.txt
Is this a Directory? false
Is this a File? true
Is this a hidden file false
Is this File readable ? true
Is this File writable ? true
File size ? 0

Program Analysis

Please share this article -

< Prev
Next >
< Byte Streams
File Input Stream>


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition