Using file.delete() to delete a file in Java IO

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.


import java.io.*;
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();
}
}
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.


------------------------------
Output
------------------------------
The file to be deleted is myfile.txt


Posted by at on

Tags: Java IO,