File Class in Java

Java File Class represents file in java program. 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

  1) The last time a file or directory was modified.

  2) The length of a file or directory.

  3) To check if a file or adirectory is writable.

  4) To check if a file or directory is read only.

  5) To check if a file or directory is executable or not.

  6 ) We can even set access restrictions like making a file or directory read only/writable.

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


Constructors of File Class

A few most used constructors of File class are explained with an example below :

Constructor Description
File(String pathname) It creates a new File instance by converting the given pathname string into an abstract pathname.
File(File parent, String child) It creates a new File instance from a parent abstract pathname and a child pathname string.
File(String parent, String child) It creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri) It creates a new File instance by converting the given file: URI into an abstract pathname.

Useful Methods of File class

Commonly used method of File class are as follows

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 doesn 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.
ong 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.


import java.io.File;
import java.io.IOException;

public class FileExample {
    public static void main(String[] args) {
        try {
            //create the object of file class
            File file = new File("Myfile.java");
            System.out.println("This file is exists or not : " + file.exists());

            //create new file
            file.createNewFile();

            System.out.println("Name of the file is : " + file.getName());
            System.out.println("Parent of the file is : " + file.getParent());
            System.out.println("Path of the file is : " + file.getPath());
            System.out.println("Absolute path of the file is : " + 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 is hidden ? " + file.isHidden());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
This file is exists or not : true
Name of the file is : Myfile.java
Parent of the file is : null
Path of the file is : Myfile.java
Absolute path of the file is : /home/durgesh/IdeaProjects/Java-IO/Myfile.java
Is this a Directory ? false
Is this a File ? true
Is this is hidden ? false

A file is actually created when createNewFile() method is called and this method is called inside a try-catch block, in order to handle any I/O error(if it occurs) while creating a file.

Program to find out the all files and folders of parent folder


import java.io.File;

//Program to find out the all files and folder of parent folder
public class ListFiles {
    public static void main(String[] args) {
        File file = new File("/home/durgesh/");
        File[] files = file.listFiles();
        for (File f : files) {
            System.out.println("Name : " + f.getName() + " : Is File ? " + f.isFile());
        }
    }
}
Name : csr.csr : Is File ? true
Name : NetBeansProjects : Is File ? false
Name : .dbus : Is File ? false
Name : tomcat : Is File ? false
Name : .layout : Is File ? true
Name : .bash_history : Is File ? true
Name : .bash_logout : Is File ? true
Name :  : Is File ? true
Name : .android : Is File ? false
Name : examples.desktop : Is File ? true
Name : .putty : Is File ? false
Name : .mysql_history : Is File ? true
Name : PycharmProjects : Is File ? false
Name : AndroidStudioProjects : Is File ? false
Name : .pki : Is File ? false
Name : .codeblocks : Is File ? false
Name : .gnupg : Is File ? false
Name : dumps : Is File ? false
Name : workspace : Is File ? false
Name : Documents : Is File ? false
Name : .profile : Is File ? true
Name : eclipse-work : Is File ? false
Name : .java : Is File ? false
Name : Templates : Is File ? false
Name : .IdeaIC2018.1 : Is File ? false
Name : .thunderbird : Is File ? false
Name : .netbeans : Is File ? false
Name : .swt : Is File ? false
Name : .local : Is File ? false
Name : java_error_in_STUDIO_6465.log : Is File ? true
Name : .vscode : Is File ? false
Name : .gradle : Is File ? false
Name : .eclipse : Is File ? false
Name : snap : Is File ? false
Name : .netbeans-derby : Is File ? false
Name : Android : Is File ? false
Name : .gconf : Is File ? false
Name : .mysql : Is File ? false
Name : .sudo_as_admin_successful : Is File ? true
Name : Public : Is File ? false
Name : hs_err_pid4344.log : Is File ? true
Name : .viminfo : Is File ? true
Name : .ICEauthority : Is File ? true
Name : News : Is File ? false
Name : java_error_in_STUDIO_6287.log : Is File ? true
Name : Firefox_wallpaper.png : Is File ? true
Name : Durgesh_C_Project : Is File ? false
Name : hs_err_pid5131.log : Is File ? true
Name : .atom : Is File ? false
Name : .cache : Is File ? false
Name : .bashrc : Is File ? true
Name : .Xauthority : Is File ? true
Name : .AndroidStudio3.1 : Is File ? false
Name : .xsession-errors.old : Is File ? true
Name : .config : Is File ? false
Name : .trn : Is File ? false
Name : .wget-hsts : Is File ? true
Name : .mozilla : Is File ? false
Name : .oracle_jre_usage : Is File ? false
Name : launcher_bfb.png : Is File ? true
Name : .m2 : Is File ? false
Name : .ssh : Is File ? false
Name : Music : Is File ? false
Name : .compiz : Is File ? false
Name : .PyCharmCE2018.1 : Is File ? false
Name : Desktop : Is File ? false
Name : .xsession-errors : Is File ? true
Name : .dmrc : Is File ? true
Name : IdeaProjects : Is File ? false
Name : .scenebuilder : Is File ? false
Name : Pictures : Is File ? false
Name : Downloads : Is File ? false
Name : Videos : Is File ? false
Name : .gnome : Is File ? false
Name : .emulator_console_auth_token : Is File ? true