All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Delete File If Exists

Last Updated : Mar 11, 2024

Java Delete File If Exists

In this article we will show you the solution of java delete file if exists, a file can be deleted using Java's File class's delete() function. A file or directory denoted by an abstract pathname may be deleted using the delete() method.

A directory cannot contain any files; only empty directories can contain files that need to be erased.

Delete is also performed by File.deleteOnExit() on files and directories that are defined by abstract pathnames.

Reverse order is applied to files when they are deleted with deleteOnExit().

The file is deleted when the JVM is terminated. Delete() is a static method in the Files class of Java that removes an existing file.

When this is not the case, it throws a NoSuchFileException. Alternatively, this method can remove a directory.

However, it is required that the directory be empty when it is removed.

We will now discuss the idea of how to delete files if they exist in java with an example.

Step By Step Guide On Java Delete File If Exists :-

import java.io.IOException;
import java.nio.file.*;
public class talkerscode{
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("./test.txt");
        try {
            // Delete file or directory
            Files.delete(path);
            System.out.println("Files or directories deleted successfully");
        } catch (NoSuchFileException ex) {
            System.out.printf("No such file or directory: %s\n", path);
        } catch (DirectoryNotEmptyException ex) {
            System.out.printf("Directory %s is not empty\n", path);
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
  1. The necessary classes and packages need to be imported before file handling operations can be performed.
  2. The main method for the "talkerscode" class must be written initially.
  3. The Path object may be used to provide a path to a deleted file.
  4. Using the Files.delete(path) method, it is possible to delete any files or directories that are provided in the path.
  5. Using the Files.delete(path) method, it is possible to delete any files or directories that are provided in the path.
  6. The existence of the file or directory will be ensured by detecting the NoSuchFileException and printing a message indicating that it does not exist.
  7. This message indicates that the directory is not empty if it is thrown when the DirectoryNotEmptyException is thrown.
  8. IOException should be caught and the error message printed if there is an input or output error.

Conclusion :-

As a result, we have successfully learned how to delete files if they exist in java with an example.

The following are the main methods of deleting files in Java. Due to the fact that they both have the same parameters, it is perfectly fine to use them interchangeably.

Java file deletion is definitely something you can master after practicing a couple of times.

I hope this article on java delete file if exists helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪