Deleting a file permanently in Java is dead simple that it is just a method away from it. The file.delete() method in the java.io.File lets you do this. Here is the example.
------------------------------
Output
------------------------------
The file to be deleted is myfile.txt
import java.io.*;delete(): The delete method when called deletes the file permanently, if it exists. You can alternatively check its existence by using the exist() method in the java.io.File class.
class DeleteFile
{
public static void main(String args[])
{
File f=new File("myfile.txt");
// Print file name
System.out.println("The file to be deleted is "+f.getName());
// Delete the file
f.delete();
}
}
------------------------------
Output
------------------------------
The file to be deleted is myfile.txt