All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Delete A File In Java

Last Updated : Mar 11, 2024

How To Delete A File In Java

In this article we will show you the solution of how to delete a file in java, in Java, the File.delete() method can be used to delete a file. When called with a pathname, the delete() method deletes the file or directory denoted by the pathname.

You must be able to delete an empty directory if the pathname is a directory.

The Java programming language provides methods for deleting files using Java programs.

When a file is deleted using the Java program, it is permanently deleted without being moved to the trash or recycle bin, contrary to normal deleting operations in any operating system.

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

Step By Step Guide On How To Delete A File In Java :-

Code 1

import java.io.File;
import java.io.IOException;
public class TalkersCode
{
public static void main(String[] args)
{
try
{
File file = new File("F:\\file.txt");
file.deleteOnExit();
System.out.println("Done");
Thread.sleep(1000);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Code 2

// Java program to demonstrate delete using Files class
import java.io.IOException;
import java.nio.file.*;
public class TC Test {
 public static void main(String[] args)
 {
  try {
   Files.deleteIfExists(
    Paths.get("C:\\Users\\Mayank\\Desktop\\
   tc.txt"));
  }
  catch (NoSuchFileException e) {
   System.out.println(
    "No such file/directory exists");
  }
  catch (DirectoryNotEmptyException e) {
   System.out.println("Directory is not empty.");
  }
  catch (IOException e) {
   System.out.println("Invalid permissions.");
  }
  System.out.println("Deletion successful.");
 }
}
  1. TalkersCode is a public class and is named TalkersCode.
  2. An entry point for the program is created by creating the main method.
  3. There is a try-catch block between the code and the error handling code.
  4. In this example, the file path is "F:/file.txt" and the file object starts.
  5. Instructing the JVM to delete the file upon exiting a program is done with the deleteOnExit() method on the file object.
  6. Console messages are printed with the message "Done.".
  7. This method pauses the program for 1000 milliseconds (1 second) using the Thread.sleep() method.
  8. Whenever an exception is thrown, the catch block executes and prints the stack trace.

Conclusion :-

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

The Java file delete method consists of two methods, namely File.delete() and Files.deleteIfExists(path), and are defined in the java.io package and in the java.nio.file package.

When called, the delete() method does not throw a SecurityException, but returns a boolean value instead.

I hope this article on how to delete a file in java 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 🡪