Java File Class ( – Uncover the Methods & Constructors used in it!

Java File Class helps to represent a file type or directory. A file and directory are not the same, a directory is the parent folder and inside that directory, there is a file.

Java file class is mainly used for creating and listing new directories, it also has many methods to list the common attributes of files and directories. It is also used for file searching, file deletion, renaming a file, etc. In short, we can say this file class is a part of package.

Java File Class

Keeping you updated with latest technology trends, Join DataFlair on Telegram

Java File Class and I/O

  • Java file class is an abstract representation of the path names and the file directories.
  • The pathname can be abstract or string and also relative and absolute.
  • The first step should be the creation of Java file class which can be done by passing a pathname or directory to it.
  • The abstract pathname which is represented by a Java file name will never change once it creates, i.e. it is immutable.

Take a break and revise the concept of Methods in Java.

How to create a Java file object?

It is created by passing a string with the name of a file, string, or another file object.

File a = new File(“/usr/local/bin/dataFlair”);

Constructors in Java

1. File (File parent, String child)

It is used to create a new file instance which is derived from an abstract parent abstract pathname and child pathname string.

2. File (String pathname)

It is used to convert the denoted pathname string into an abstract pathname and thus create a new file instance.

3. File (String parent, String child)

It is used to create a new file instance using a parent and child pathname.

4. File (URI uri)

It is used to create a file instance by converting the given URL into an abstract pathname.

Learn more about Constructor in Java with Examples

Java Methods

Name of method Description
canExecute() Used to test whether the denoted file can be executed by the application using this abstact pathname.
canRead() Test whether the denoted file can be read using this abstract pathname.
canWrite() Used to test whether the file denoted can be modified using this pathname or not.
compareTo(File pathname) Used to compare two pathnames on the basis of lexicography.
createNewFile() Automatically creates a new file which is empty with this username.
File createTempFile(String prefix, String suffix) Creates an empty file it the file default directory.
delete() Deletes the file which has this pathname.
equals(Object obj) Tests for equality with the given pathname and the object.
exists() To test whether the given file or directory exists or not with the same abstract pathname.
getAbsolutePath() Returns the absolute pathname for the abstract pathname.
getFreeSpace() Returns the count of the unallocated bytes in the partition.
getName() Returns the path of the file which is denoted by this abstract filename.
getParent() This method returns the pathname string of the file which is denoted by this abstract filename’s parent.
getParentFile() This method returns the abstract pathname string of the file which is denoted by this abstract filename’s parent.
getPath() Converts the abstract pathname into a pathname.
isDirectory() Check for directory, whether the given pathname is a directory or not.
isFile() To test whether the given abstract file is a normal file or not.
isHidden() This method tests whether the file given is hidden or not.
length() To return length of the file.
String[] list() Returns an array of strings which has the names of the files and directories.
File[] listFiles() Denote the names of the abstract pathnames in the directory.
 mkdir() To create a directory with this name.
 renameTo(File dest) To rename the file with this abstract pathname.
 setExecutable(boolean executable) This method is used to set owner’s executing permission.
 setReadable(boolean readable) This method is used to set an owner’s reading permission.
 setReadable(boolean readable, boolean ownerOnly) This method is used to set owner’s or for a matter of fact, anybody’s executing permission.
setReadOnly() Set the directory named so that only read permissions are given.
setWritable(boolean writable) Set the directory named so that only writing permissions are given.
String toString() Return the string of the pathname.
URI toURI() Construct a file URI representing this pathname.

Implementation of Java Methods

Example 1 – In this program, We are going to create a file. Java program will check if that file or directory physically exist or not.

package com.DataFlair.JavaFileClass;
public class FileClassDemo {
      public static void main(String[] args) {  
          String path = "";  
          boolean bool = false;  
          try {  
              // creating  new files  
              File file  = new File("JavaTestFile.txt");  
              // creating new canonical from file object  
              File file2 = file.getCanonicalFile();  
              // returns true if the file exists  
              bool = file2.exists();  
              // returns absolute pathname  
              path = file2.getAbsolutePath();  
              // if file exists  
              if (bool) {  
                  // prints  
                  System.out.print(path + " Exists? " + bool);  
          } catch (Exception e) {  
              // if any error occurs  

Output- File-Class-Demo

Example 2 – This java program checks whether a particular file exists on a specified path or not.

package com.DataFlair.JavaFileClass;
public class FileClassDemo1 {
       public static void main(String[] args) {  
          try {  
              File file = new File("DataFlairJavaProgramFile.txt");  
              if (file.createNewFile()) {  
                  System.out.println("New File is created!");  
              } else {  
                  System.out.println("File already exists.");  
          } catch (IOException e) {  




To sum up, we can say that Java file class is used to represent the file and directory pathname. As file and directory, names have various formats on various platforms.

Now, you knew about methods and constructors with their implementation example. Hope, you liked the article. Please share your experience with us!

Your Next Move – Constructor Overloading in Java

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.