Java

How to delete a file in Java

Java provides a File class that contains an extensive number of built-in file handling methods such as createNewFile(), mkdir(), getAbsolutePath() and so on. Each method performs some specific functionality, for example createNewFile() creates a new empty file, mkdir() creates a directory, etc. If we talk about the file deletion, the File class provides the delete() method that can be used either to delete a file or to remove an empty folder.

This write-up will focus on the delete() method and in this regard, it will cover the following aspects:

So, let’s get started!

What is a delete() method

It is a built-in method of File class that can be utilized to delete/remove some specific file or an empty directory. The delete() method wouldn’t move the specified file or directory to the recycle bin; instead it will delete them permanently.

How to work with delete() method

The very first thing that we need to do is import the File class from java.io package and to do so, we have to utilize the import keyword:

import java.io.File;

After importing the File class, we can create the object of that class, and then we can avail all the functionalities of the File class.

How to Delete a File

To delete a file all we need to do is import the File class, create the object of that class and specify the name of the file within the parenthesis. Next, utilize the object of the class with the delete() method to remove the specified file.

Example

Let’s consider the below code snippet for the profound understanding of the delete() method in java:

File deleteObj = new File("C:\\Users\\DELL\\Desktop\\text.txt");       
  if (deleteObj.delete())
  {
     System.out.println("File Deleted");
  } else {
     System.out.println("File Not Deleted");
 }

The complete code and respective output will look like this:

The above code verifies that importing the File class allows us to create the object of that class. The output section authenticates that the delete() method succeeds in deleting the specified file.

How to delete a Folder in Java

We can utilize the delete() method to delete an empty directory/folder. If a folder contains some files and we have to delete that non-empty folder then we have to delete its files first.

Example

The below-given snippet creates the object of the file class and specifies the directory name within the parenthesis. Afterward, we utilize the object of that class to delete the specified directory.

File deleteObj = new File("C:\\Users\\DELL\\Desktop\\JavaFolder");
  if (deleteObj.delete()) {
        System.out.println("Directory: " + deleteObj.getName() + " Deleted");
    } else {
       System.out.println("Not Deleted");
 }

In the above snippet, we utilize the getName() method to get the name of the specified folder. The below-given screenshot provides the complete code and its respective output:

The above output validates that the delete() method successfully deletes the specified empty directory.

Conclusion

In java, the delete() method of the File class can be utilized to remove a specific file or an empty folder permanently. The File class can be imported using the import keyword and importing the File class will make it possible to create the object of that class. The name and path of the file or directory will be specified at the time of object creation and afterward, the delete() method can be utilized with that object to delete the specified file or directory. This tutorial provides a step-by-step guide for how to work with the delete() method in order to remove a file or directory permanently.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.