File Class Tutorial in Java

Introduction

 
Java File class provides several methods to work on file system like exists(), createNewFile(), canWrite(), getName() etc. In this article, we will learn about the Java File class, its basic methods, and constructors provided by Java programming language.
 

File Class in Java

 
File class in Java Programming language is an abstract representation of file and directory abstract pathnames. Java provides a File class for working with the file in our system. Java File class is used to access the information about the file that we have saved in our system. we can access all the characteristics of the file from our system through the File Class.
 
File class provides many methods for access the data about the file from the system through the Java programs. The object of File class is used to access the information about the file from the system.
 

Use of File Class in Java

  • Check-in system that particular file exists or not.
  • Creates a new file if it does not exists at the given path.
  • It can check the name of the file and rename the file in the system
  • The object of the File class accesses the data of the file.
  • Can find out the size of the file.
  • Check the type of file, like writeable or not.
The complete example program of java.io.File class is listed below.
  1. import java.io.File;  
  2. //import File class by this statement.  
  3.   
  4. public class FileClassExample1 {  
  5.     public static void main(String [] args) {  
  6.   
  7.         //creates a object named f1 and use File("String") constructor .  
  8.         File f1= new File("D:\\studymaterial\\java notes\\file1.txt");  
  9.         //Here we check that filename has given is exist on the same path or not.  
  10.         System.out.println(f1.exists());  
  11.         //Let's see the output of the program  
  12.     }  

The output of the above program generates the following output.
 
FileClassExample1
 
Explanation
 
In the above program, first, we import File class by the statement import java.io.File; Then creates the object of the File class named f1, through this object we call the exists()  method of File class by the statement f1.exists(). This method checks that the particular file exists in the system or not.
 

How to create File Class Object in Java?

 
A file object is created by passing a file name or a string or other file object into a string. For example:
  1. File f1= new File("D:\\studymaterial\\java notes\\file1.txt");   
In this f1 is the object of File class and as a parameter "D:\\studymaterial\\java notes\\file1.txt". This is abstract file name.
 

Methods of File Class Java

 
The java.io.File class contains various methods for performing basic operations with the file system in our computer system. The various Java File methods are as follows:
 

1. File.exists() Method in Java

 
File.exists() method is used to check a particular file or directory exists in the system or not. It takes a parameter in the form of String. This String parameter is the path of the drive, directory, or file where we want to check the existence of the file or directory. It does not throw any exception. 
 
The complete program of the File.exists() method is listed below:
  1. import java.io.File;  
  2.   
  3. public class FileExistExample {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.   
  9.         if(f1.exists())  
  10.         {  
  11.             System.out.println("File exist in the system");  
  12.         }  
  13.         else  
  14.             {  
  15.             System.out.println("file does not exist");  
  16.             }  
  17.     }  

The above program generates the following output.
 
FileExistsExampleOutput
 
Explanation
 
In the above program, we check that the file named file1.txt exists at the given location through the if-else statement.
 
Another program of File.exists() method is listed below, here we will check a particular directory exist in the system or not.
  1. import java.io.File;  
  2.   
  3. public class FileExistsExample1 {  
  4.   
  5.     public static void main(String[] args) {  
  6.         File f1 = new File("D:\\studymaterial\\java notes");  
  7.   
  8.         if (f1.exists()) {  
  9.             System.out.println("Directory exist in the system");  
  10.         } else {  
  11.             System.out.println("Directory does not exist");  
  12.         }  
  13.     }  

The above program generates the following output.
 
FileDirectoryExistsExample
 

2. File.createNewFile() in Java

 
File.createNewFile() method is used to create a new file in the system. It takes a parameter in the form of String. This String parameter is the path of the drive or directory where we want to create the file with the given name. It through an exception. So, we use a try-catch statement for handling the exception.
 
The complete program of File.createNewFile() method is listed below.
  1. import java.io.File;  
  2. import java.io.*;  
  3.   
  4. public class FileCreateNewFileExample {  
  5.     public static void main(String [] args) {  
  6.         File f1= new File("D:\\studymaterial\\java notes\\file1.txt");  
  7.         try {  
  8.             if (f1.exists()) {  
  9.                 //checks the file exist in the system or not  
  10.                 System.out.println("File already Exists!");  
  11.                   
  12.                 //if file exists in the system it will execute the if statement, that File already exists.  
  13.                 /*If the file does not exist it will print the else statement and create a new file 
  14.                 at the location D:\studymaterial\java notes\file1.txt with the name file1.txt*/  
  15.   
  16.             } else if(f1.createNewFile()){  
  17.   
  18.                 System.out.println("New file created!");  
  19.             }  
  20.         } catch (IOException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.         //file with the name file1.txt does not exist at the given location so it creates the new file  
  24.         // as the output of the program  
  25.     }  

The above program generates the following output.
 
FileCreateNewFileExample
 
Explanation
 
In the above program, first, we check the particular file exists in the system or not by the if statement. we get the result that file does not exist so else statement executed and as the output New file named file1.txt is created at the location D:\\studymaterial\\java notes\\file1.txt.
 
newFileCreated
 

3. File.getName() Method in Java

 
File.getName() method in Java is used to get the name of a particular file through the Java program. It does not take any argument. It does not throw any exception. This method returns the file name as the output.
 
The complete program of the File.getName() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileGetNameExample {  
  4.     public static void main(String[] args) {  
  5.   
  6.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  7. System.out.println("File Name : " +f1.getName());  
  8.   
  9.     }  

The above program generates the following output.
 
filegetNameExample
 
Explanation
 
In the above program, we use the File.getName() method which returns the name of the file. 
 

4. File.canExecute() Method in Java

 
File.canExecute() method in Java is used to check that the Java program can execute the file or not. It does not take any parameter. This method does not throw any exception. File.canExecute() method returns true or false as the output.  
 
The complete program of File.canExecute() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileCanExecute {  
  4.         public static void main(String[] args) {  
  5.   
  6.             File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  7.             System.out.println("File can be executed : " +f1.canExecute());  
  8.   
  9.         }  
  10.     } 
The above program generates the following output.
 
FileCanExecutesExample
 
Explanation
 
In the above program, we check that the Java program can execute the file or not. so the result method returns true. That means the program can be executed.
 

5. File.canWrite() Method in Java

 
File.canWrite() method in Java is used to check that the Java program can modify the file that has been given by the abstract pathname. It method does not take any parameter. This method does not throw any exception.
 
It returns true or false as the output.
 
The complete program of the File.canWrite() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FilecanWriteExample {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.         System.out.println("File can be modified by the Java application : " + f1.canWrite());  
  9.   
  10.     }  
  11.   

The above program generates the following output.
 
FilecanWriteExample
 
Explanation
 
In the above program, we check that the Java program can write or modify the file that has been given by the abstract pathname. The program generates the true as the output, it means we can modify the file.
 

6. File.canRead() Method in Java

 
File.canRead() method in Java is used to check that the Java program can read in the file that has been given by the abstract pathname. It method does not take any parameter. This method does not throw any exception. It returns true or false as the output.
 
The complete program of File.canRead() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileCanReadExample {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.         System.out.println("Can we read the file by the Java application ? " + f1.canRead());  
  9.   
  10.     }  
  11.   
  12. }  
The above program generates the following output.
 
FileCanReadExample
 
Explanation
 
In the above program, we check that the Java program can read the file that has been given by the abstract pathname. The program generates the true as the output, it means we can read the data in the file.
 

7.File.length() Method in Java

 
File.length() method in Java is used to check the size of the file by the Java program. The method returns the length of the file, it returns the long value that represents the number of bits if the file has any data if the does not have any data or file does not exist it returns 0L.
 
It returns the length of the file as the output. The length() method does not take any parameter.
 
The complete program of File.length() method in Java is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileLengthExample {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.         System.out.println("length of the file is : " + f1.length());  
  9.   
  10.     }  
  11.       

The above program generates the following output.
 
FileLengthExample
 
Explanation
 
In the above program, we check the length of the file and the output is 0 because it is an empty file.
 
File1txtExample
 
As we add some data into the file of named file1.txt. Now we will check the length of the file through the File.length() method.
 
Another example program of File.length() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileLengthExample2 {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.         System.out.println("length of the file is : " + f1.length());  
  9.   
  10.     }  
  11.   

The above program generates the following output.
 
FileLengthExample1
 
Explanation
 
In the above program, we check the length of the file after adding some data into the file. As the output, the program returns 41, the program counts data in the form of characters.
 

8. File.getFreeSpace() Method in Java

 
File. getFreeSpace() method is used to count the free spaces or unallocated bytes in the file through the Java program. This program does not take any parameters. It returns the number of free spaces from the file as the output. It does not throw any exception. It returns the output in the long value.
 
The complete program of File.getFreeSace() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileGetFreeSpace {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.         System.out.println("Free space in the file is : " + f1.getFreeSpace());  
  9.   
  10.     }  

The above program generated the following output.
 
FilegetFreeSpaceExample
 
Explanation
 
In the above program, we use File.getFreeSpace() method that returns the unallocated spaces in the file in the long value.
 

9. File.getParent() Method in Java

 
File.getParent() method is used to get the parent pathname of the file which is given as the abstract pathname. This method does not take any parameter and does not throw any exception.
 
The complete program of File.getParent() method is listed below.
  1. import java.io.File;  
  2.   
  3. public class FileGetParentExample {  
  4.   
  5.     public static void main(String[] args) {  
  6.   
  7.         File f1 = new File("D:\\studymaterial\\java notes\\file1.txt");  
  8.         System.out.println("Get parent method example : " + f1.getParent());  
  9.   
  10.     }  

The above program generates the following output.
 
FileGetParentExample
 
Explanation
 
The above program returns the "D:\\studymaterial\java notes" as the output, it is parent pathname of the file "file1.txt".
 

Summary

 
In this article, we learned about File class and its methods in Java programming language and how we can use them in Java Programs.