Java Program to Delete a File

You are currently viewing Java Program to Delete a File

Java program to delete a file

// Java program to get file size 
// and file path


public class ExDeleteFile {
  public static void main(String args[]) {
    final String fileName = "file3.txt";

    //File class object
    File objFile = new File(fileName);

    //check file is exist or not, if exist delete it
    if (objFile.exists() == true) {
      //file exists
      //deleting the file
      if (objFile.delete()) {
        System.out.println(objFile.getName() + " deleted successfully.");
      } else {
        System.out.println("File deletion failed!!!");
    } else {
      System.out.println("File does not exist!!!");


file3.txt deleted successfully.


Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply