In this tutorial, I am sharing simple program about How to delete a file in Java.

To delete the file using Java, We have to call the predefined method called delete(). Which is actually belongs to java.io.File class. This is method will return boolean value such as true If the file is deleted successfully from the file system. It will return false If the file is not exist in the specified path.

FileDeletionExample.java

package com.dineshkrish.files;
import java.io.File;
public class FileDeletionExample {
public static void main(String[] args) {
// Defining the File Object
File file = new File("abc.txt");
// Calling the method delete()
boolean flag = file.delete();
if(flag) {
System.out.println("The File is Deleted Successfully!!!!");
} else {
System.out.println("The File is not exist.");
}		
}
}

References

1. Java API Documentation

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *